What if you could find all words starting with your letters instantly, no matter how big the list?
Why Count Words with Given Prefix in DSA Java?
Imagine you have a huge list of words, like a dictionary, and you want to find out how many words start with a certain set of letters, like "pre". Doing this by checking each word one by one is like looking for friends in a crowd by calling their full names out loud--slow and tiring.
Manually scanning every word to check if it starts with the prefix wastes a lot of time, especially if the list is very long. It's easy to make mistakes or miss some words because you have to look at each word fully. This method doesn't scale well and becomes frustrating.
Using a smart structure like a Trie (prefix tree) lets you quickly jump to the part where words with the prefix live. It's like having a special directory that groups words by their starting letters, so you don't have to check every word. This makes counting words with a given prefix fast and easy.
int count = 0; for (String word : words) { if (word.startsWith(prefix)) { count++; } }
int count = trie.countWordsWithPrefix(prefix);
This lets you instantly find how many words share the same beginning, enabling fast search features like autocomplete and spell-check.
When you type in a search box, the system suggests words starting with what you typed. This is possible because it quickly counts and finds words with that prefix using this method.
Manually checking each word for a prefix is slow and error-prone.
Using a Trie structure groups words by prefixes for fast counting.
This method powers quick search suggestions and autocomplete.
Practice
Solution
Step 1: Understand the prefix concept
The prefix is the starting part of a word. We need to check if words start with this prefix.Step 2: Count matching words
We count all words that begin with the given prefix from the list.Final Answer:
Count how many words in a list start with a specific prefix -> Option AQuick Check:
Prefix means start of word = Count matching words [OK]
- Counting words containing prefix anywhere, not just start
- Confusing prefix with suffix
- Counting characters instead of words
word starts with a prefix pref?Solution
Step 1: Recall Java String methods
Java'sstartsWithmethod checks if a string begins with a given prefix.Step 2: Match method to requirement
Since we want to check the start,word.startsWith(pref)is correct.Final Answer:
word.startsWith(pref) -> Option DQuick Check:
startsWith checks prefix = word.startsWith(pref) [OK]
- Using contains() which checks anywhere in string
- Using equals() which checks full string equality
- Using endsWith() which checks suffix
String[] words = {"apple", "app", "application", "banana"};
String prefix = "app";
int count = 0;
for (String w : words) {
if (w.startsWith(prefix)) {
count++;
}
}
System.out.println(count);Solution
Step 1: Check each word against prefix "app"
Words starting with "app" are "apple", "app", and "application" (3 words).Step 2: Count matching words
Count increments 3 times for these words.Final Answer:
3 -> Option AQuick Check:
3 words start with "app" = 3 [OK]
- Counting only exact matches
- Counting words containing prefix anywhere
- Off-by-one errors in counting
String[] words = {"cat", "car", "dog", "cart"};
String prefix = "car";
int count = 0;
for (int i = 0; i <= words.length; i++) {
if (words[i].startsWith(prefix)) {
count++;
}
}
System.out.println(count);Solution
Step 1: Analyze loop condition
The loop runs from i = 0 to i <= words.length, which causes an IndexOutOfBoundsException because last index is words.length - 1.Step 2: Correct loop condition
Change condition to i < words.length to avoid accessing invalid index.Final Answer:
Loop condition should be i < words.length, not i <= words.length -> Option CQuick Check:
Array index max is length-1 = fix loop condition [OK]
- Using <= instead of < in loop condition
- Ignoring array index limits
- Misusing startsWith method
["tree", "trie", "trigger", "trip", "trick"] and prefix "tri", which Java code snippet correctly counts the words starting with the prefix using streams?Solution
Step 1: Use stream filter with startsWith
The filter should keep words starting with the prefix usingw.startsWith(prefix).Step 2: Count filtered words
Usecount()to get the number of matching words. The result is a long type.Final Answer:
long count = Arrays.stream(words).filter(w -> w.startsWith(prefix)).count(); -> Option BQuick Check:
Filter with startsWith + count() = long count = Arrays.stream(words).filter(w -> w.startsWith(prefix)).count(); [OK]
- Using map instead of filter
- Using contains instead of startsWith
- Using endsWith instead of startsWith
Ready to prove it?
Pass the test and this pattern moves unknown
