Skip to content

Commit

Permalink
Solution to "String Matching in an Array" problem
Browse files Browse the repository at this point in the history
  • Loading branch information
dkoval committed Jan 7, 2025
1 parent 34b2ed7 commit 6ec9ddc
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.github.dkoval.leetcode.challenge;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
* <a href="https://leetcode.com/problems/string-matching-in-an-array/">String Matching in an Array</a>
* <p>
* Given an array of string words, return all strings in words that is a substring of another word. You can return the answer in any order.
* <p>
* A substring is a contiguous sequence of characters within a string.
* <p>
* Constraints:
* <ul>
* <li>1 <= words.length <= 100</li>
* <li>1 <= words[i].length <= 30</li>
* <li>words[i] contains only lowercase English letters.</li>
* <li>All the strings of words are unique.</li>
* </ul>
*/
public interface StringMatchingInArray {

List<String> stringMatching(String[] words);

class StringMatchingInArrayRev1 implements StringMatchingInArray {

@Override
public List<String> stringMatching(String[] words) {
final var n = words.length;

final var ans = new HashSet<String>();
for (var i = 0; i < n; i++) {
for (var j = 0; j < n; j++) {
if (j != i) {
if (substring(words[j], words[i])) {
ans.add(words[j]);
}
}
}
}
return new ArrayList<>(ans);
}

// check if s1 is a substring of s2
private boolean substring(String s1, String s2) {
final var w1 = s1.length();
final var w2 = s2.length();

if (w1 > w2) {
return false;
}

for (var offset = 0; offset <= w2 - w1; offset++) {
var found = true;
for (var i = 0; i < w1; i++) {
if (s1.charAt(i) != s2.charAt(i + offset)) {
found = false;
break;
}
}

if (found) {
return true;
}
}
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.github.dkoval.leetcode.challenge

import com.github.dkoval.leetcode.challenge.StringMatchingInArray.StringMatchingInArrayRev1
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.extension.ExtensionContext
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.ArgumentsProvider
import org.junit.jupiter.params.provider.ArgumentsSource
import java.util.stream.Stream

internal class StringMatchingInArrayTest {

class InputArgumentsProvider : ArgumentsProvider {

override fun provideArguments(context: ExtensionContext): Stream<out Arguments> = Stream.of(
Arguments.of(
arrayOf("mass", "as", "hero", "superhero"),
listOf("as", "hero")
),
Arguments.of(
arrayOf("leetcode", "et", "code"),
listOf("et", "code")
),
Arguments.of(
arrayOf("blue", "green", "bu"),
emptyList<String>()
),
Arguments.of(
arrayOf("leetcoder", "leetcode", "od", "hamlet", "am"),
listOf("leetcode", "od", "am")
)
)
}

@Nested
inner class StringMatchingInArrayRev1Test {

@ParameterizedTest
@ArgumentsSource(InputArgumentsProvider::class)
fun `should return all strings in words that is a substring of another word`(
words: Array<String>,
expected: List<String>
) {
StringMatchingInArrayRev1().test(words, expected)
}
}
}

private fun StringMatchingInArrayRev1.test(words: Array<String>, expected: List<String>) {
val actual = stringMatching(words)
assertThat(actual).containsExactlyInAnyOrderElementsOf(expected)
}

0 comments on commit 6ec9ddc

Please sign in to comment.