-
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 "Counting Words With a Given Prefix" problem
- Loading branch information
Showing
2 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
src/main/java/com/github/dkoval/leetcode/challenge/CountingWordsWithGivenPrefix.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,50 @@ | ||
package com.github.dkoval.leetcode.challenge; | ||
|
||
/** | ||
* <a href="https://leetcode.com/problems/counting-words-with-a-given-prefix/">Counting Words With a Given Prefix</a> | ||
* <p> | ||
* You are given an array of strings words and a string pref. | ||
* <p> | ||
* Return the number of strings in words that contain pref as a prefix. | ||
* <p> | ||
* A prefix of a string s is any leading contiguous substring of s. | ||
* <p> | ||
* Constraints: | ||
* <ul> | ||
* <li>1 <= words.length <= 100</li> | ||
* <li>1 <= words[i].length, pref.length <= 100</li> | ||
* <li>words[i] and pref consist of lowercase English letters.</li> | ||
* </ul> | ||
*/ | ||
public interface CountingWordsWithGivenPrefix { | ||
|
||
int prefixCount(String[] words, String pref); | ||
|
||
// O(W * P) time | O(1) space | ||
class CountingWordsWithGivenPrefixRev1 implements CountingWordsWithGivenPrefix { | ||
|
||
@Override | ||
public int prefixCount(String[] words, String pref) { | ||
var count = 0; | ||
for (var word : words) { | ||
if (prefix(word, pref)) { | ||
count++; | ||
} | ||
} | ||
return count; | ||
} | ||
|
||
private boolean prefix(String word, String pref) { | ||
if (word.length() < pref.length()) { | ||
return false; | ||
} | ||
|
||
for (var i = 0; i < pref.length(); i++) { | ||
if (word.charAt(i) != pref.charAt(i)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/test/kotlin/com/github/dkoval/leetcode/challenge/CountingWordsWithGivenPrefixTest.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,49 @@ | ||
package com.github.dkoval.leetcode.challenge | ||
|
||
import com.github.dkoval.leetcode.challenge.CountingWordsWithGivenPrefix.CountingWordsWithGivenPrefixRev1 | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
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 CountingWordsWithGivenPrefixTest { | ||
|
||
class InputArgumentsProvider : ArgumentsProvider { | ||
|
||
override fun provideArguments(context: ExtensionContext): Stream<out Arguments> = Stream.of( | ||
Arguments.of( | ||
arrayOf("pay", "attention", "practice", "attend"), | ||
"at", | ||
2 | ||
), | ||
Arguments.of( | ||
arrayOf("leetcode", "win", "loops", "success"), | ||
"code", | ||
0 | ||
) | ||
) | ||
} | ||
|
||
@Nested | ||
inner class CountingWordsWithGivenPrefixRev1Test { | ||
|
||
@ParameterizedTest | ||
@ArgumentsSource(InputArgumentsProvider::class) | ||
fun `should Return the number of strings in words that contain pref as a prefix`( | ||
words: Array<String>, | ||
pref: String, | ||
expected: Int | ||
) { | ||
CountingWordsWithGivenPrefixRev1().test(words, pref, expected) | ||
} | ||
} | ||
} | ||
|
||
private fun CountingWordsWithGivenPrefix.test(words: Array<String>, pref: String, expected: Int) { | ||
val actual = prefixCount(words, pref) | ||
assertEquals(expected, actual) | ||
} |