Skip to content

Commit

Permalink
Solution to "Counting Words With a Given Prefix" problem
Browse files Browse the repository at this point in the history
  • Loading branch information
dkoval committed Jan 9, 2025
1 parent 77252f9 commit 69ab275
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
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;
}
}
}
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)
}

0 comments on commit 69ab275

Please sign in to comment.