-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Solution to "String Matching in an Array" problem
- Loading branch information
Showing
2 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
src/main/java/com/github/dkoval/leetcode/challenge/StringMatchingInArray.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/test/kotlin/com/github/dkoval/leetcode/challenge/StringMatchingInArrayTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |