Count Words with Given Prefix in DSA Rust - Time & Space Complexity
We want to understand how the time needed to count words with a certain prefix grows as the list of words gets bigger.
How does the number of steps change when we have more words or longer prefixes?
Analyze the time complexity of the following code snippet.
fn count_prefix(words: &[&str], prefix: &str) -> usize {
let mut count = 0;
for &word in words {
if word.starts_with(prefix) {
count += 1;
}
}
count
}
This code counts how many words in the list start with the given prefix.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each word to see if it starts with the prefix.
- How many times: Once for every word in the list.
As the number of words grows, the time to check all words grows roughly the same amount.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 prefix checks |
| 100 | About 100 prefix checks |
| 1000 | About 1000 prefix checks |
Pattern observation: The work grows directly with the number of words.
Time Complexity: O(n * m)
This means the time grows with the number of words (n) and the length of the prefix (m) because each check compares up to m characters.
[X] Wrong: "Checking prefixes is always O(1) because it's just a quick check."
[OK] Correct: Each prefix check compares characters one by one, so longer prefixes take more time, making it depend on prefix length.
Understanding how prefix checks scale helps you explain and improve search features, a common task in coding interviews.
"What if the words were stored in a trie (prefix tree)? 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 given prefix, not about length or sorting.Step 2: Identify the key task
Counting words that begin with the prefix is the main task.Final Answer:
Count how many words in a list start with a specific prefix -> Option BQuick Check:
Count words starting with prefix = Count how many words in a list start with a specific prefix [OK]
- Confusing prefix with substring anywhere in word
- Counting total characters instead of words
- Sorting words instead of counting
prefix?Solution
Step 1: Recall Rust string methods
Rust'sstarts_withmethod checks if a string begins with a given substring.Step 2: Compare options
containschecks anywhere,ends_withchecks suffix, and==checks full equality, so onlystarts_withfits.Final Answer:
word.starts_with(prefix) -> Option AQuick Check:
Use starts_with() to check prefix [OK]
- Using contains() which matches anywhere, not just prefix
- Using ends_with() which checks suffix
- Comparing full equality instead of prefix
let words = vec!["apple", "app", "application", "banana"];
let prefix = "app";
let count = words.iter().filter(|w| w.starts_with(prefix)).count();
println!("{}", count);Solution
Step 1: Identify words starting with "app"
Words are "apple", "app", "application", and "banana". The first three start with "app".Step 2: Count matching words
Count is 3 because "apple", "app", and "application" start with "app".Final Answer:
3 -> Option AQuick Check:
3 words start with "app" [OK]
- Counting only exact matches
- Including words that don't start with prefix
- Off-by-one counting errors
let words = vec!["cat", "car", "dog", "scare"];
let prefix = "car";
let count = words.iter().filter(|w| w.contains(prefix)).count();
println!("{}", count);Solution
Step 1: Understand difference between contains() and starts_with()
contains()checks if prefix appears anywhere, not just at start.Step 2: Identify incorrect matches
"scare" contains "car" but does not start with "car", so contains() wrongly counts it.Final Answer:
Using contains() instead of starts_with() causes wrong matches -> Option DQuick Check:
Use starts_with() to count prefix matches [OK]
- Confusing contains() with starts_with()
- Assuming contains() only matches prefix
- Ignoring method differences
["test", "tester", "testing", "toast", "team"] and prefix "te", which Rust code correctly counts words starting with the prefix and ignores case sensitivity?Solution
Step 1: Understand case sensitivity in Rust strings
Rust'sstarts_withis case sensitive, so we convert both word and prefix to lowercase.Step 2: Check each option for case-insensitive prefix check
words.iter().filter(|w| w.to_lowercase().starts_with(&prefix.to_lowercase())).count() converts both to lowercase before checking prefix, ensuring case-insensitive match. Others do not handle case.Final Answer:
words.iter().filter(|w| w.to_lowercase().starts_with(&prefix.to_lowercase())).count() -> Option CQuick Check:
Convert both strings to lowercase before starts_with() [OK]
- Ignoring case sensitivity in starts_with()
- Using contains() which is wrong for prefix
- Using ends_with() instead of starts_with()
Ready to prove it?
Pass the test and this pattern moves unknown
