Count Words with Given Prefix in DSA C - Time & Space Complexity
We want to understand how the time needed to count words with a certain prefix grows as the input changes.
Specifically, how does the work increase when we have more words or longer prefixes?
Analyze the time complexity of the following code snippet.
int countPrefix(char *words[], int n, char *prefix) {
int count = 0;
int prefixLen = strlen(prefix);
for (int i = 0; i < n; i++) {
if (strncmp(words[i], prefix, prefixLen) == 0) {
count++;
}
}
return count;
}
This code counts how many words in the array start with the given prefix.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop through all words and compare prefix characters.
- How many times: The loop runs once for each word (n times), and each comparison checks up to prefix length characters.
As the number of words (n) grows, the code checks each word once. For each word, it compares characters up to the prefix length.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 x prefix length comparisons |
| 100 | About 100 x prefix length comparisons |
| 1000 | About 1000 x prefix length comparisons |
Pattern observation: The work grows linearly with the number of words and linearly with the prefix length.
Time Complexity: O(n * m)
This means the time grows proportionally with the number of words (n) and the length of the prefix (m).
[X] Wrong: "The time depends only on the number of words, not the prefix length."
[OK] Correct: Each word comparison checks characters up to the prefix length, so longer prefixes mean more work per word.
Understanding how input size and prefix length affect performance helps you explain your code clearly and choose better approaches if needed.
"What if the words were stored in a trie data structure? How would the time complexity change?"
Practice
Solution
Step 1: Understand the problem statement
The problem asks to find how many words start with a specific prefix string.Step 2: Identify the key operation
We need to check each word and count those that begin with the given prefix.Final Answer:
Count how many words in a list start with a given prefix -> Option AQuick Check:
Count words starting with prefix = Count how many words in a list start with a given prefix [OK]
- Counting total characters instead of words
- Looking for words containing prefix anywhere
- Sorting words instead of counting
prefix?Solution
Step 1: Understand string comparison functions
strncmp compares first n characters; here n = length of prefix.Step 2: Check which code confirms prefix match
strncmp(word, prefix, strlen(prefix)) == 0 means word starts with prefix exactly.Final Answer:
strncmp(word, prefix, strlen(prefix)) == 0 -> Option BQuick Check:
strncmp with prefix length = 0 means prefix match [OK]
- Using strcmp which requires full string match
- Using strstr without checking start position
- Comparing lengths only
char *words[] = {"apple", "app", "application", "banana"};
char prefix[] = "app";
int count = 0;
for (int i = 0; i < 4; i++) {
if (strncmp(words[i], prefix, strlen(prefix)) == 0) {
count++;
}
}
printf("%d\n", count);Solution
Step 1: Check each word against prefix "app"
"apple" starts with "app" (yes), "app" (yes), "application" (yes), "banana" (no).Step 2: Count matching words
Three words match the prefix, so count = 3.Final Answer:
3 -> Option DQuick Check:
Words starting with "app" = 3 [OK]
- Counting words that contain prefix anywhere
- Off-by-one errors in loop
- Incorrect prefix length in strncmp
prefix:int countWords(char *words[], int n, char *prefix) {
int count = 0;
for (int i = 0; i <= n; i++) {
if (strncmp(words[i], prefix, strlen(prefix)) == 0) {
count++;
}
}
return count;
}Solution
Step 1: Analyze loop boundary
Loop runs from i = 0 to i <= n, which accesses words[n] (out of bounds).Step 2: Correct loop condition
Change to i < n to avoid accessing beyond array size.Final Answer:
Loop condition should be i < n, not i <= n -> Option CQuick Check:
Array index must be less than size [OK]
- Using <= instead of < in loops
- Confusing strcmp and strncmp usage
- Wrong length argument in strncmp
pre. Which approach is best to optimize counting if you must do many prefix queries efficiently?Solution
Step 1: Understand multiple prefix queries
Checking each word with strncmp repeatedly is slow for many queries.Step 2: Choose data structure for fast prefix counting
Trie stores words by prefixes, allowing quick count of words with given prefix.Step 3: Compare alternatives
Binary search helps but trie is more direct for prefix counts; hash set doesn't support prefix queries well.Final Answer:
Build a trie (prefix tree) from words and count nodes matching prefix -> Option AQuick Check:
Trie = fast prefix count [OK]
- Using linear search for many queries
- Ignoring trie benefits for prefix problems
- Using hash set which doesn't support prefix search
Ready to prove it?
Pass the test and this pattern moves unknown
