Bird
Raised Fist0
DSA Rustprogramming~5 mins

Count Words with Given Prefix in DSA Rust - Time & Space Complexity

Choose your learning style10 modes available
Time Complexity: Count Words with Given Prefix
O(n * m)
Understanding Time 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?

Scenario Under Consideration

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 Repeating Operations

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.
How Execution Grows With Input

As the number of words grows, the time to check all words grows roughly the same amount.

Input Size (n)Approx. Operations
10About 10 prefix checks
100About 100 prefix checks
1000About 1000 prefix checks

Pattern observation: The work grows directly with the number of words.

Final Time Complexity

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.

Common Mistake

[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.

Interview Connect

Understanding how prefix checks scale helps you explain and improve search features, a common task in coding interviews.

Self-Check

"What if the words were stored in a trie (prefix tree)? How would the time complexity change?"

Practice

(1/5)
1. What does the problem "Count Words with Given Prefix" ask you to do?
easy
A. Find the longest word in a list
B. Count how many words in a list start with a specific prefix
C. Count the total number of characters in all words
D. Sort the words alphabetically

Solution

  1. Step 1: Understand the problem statement

    The problem asks to find how many words start with a given prefix, not about length or sorting.
  2. Step 2: Identify the key task

    Counting words that begin with the prefix is the main task.
  3. Final Answer:

    Count how many words in a list start with a specific prefix -> Option B
  4. Quick Check:

    Count words starting with prefix = Count how many words in a list start with a specific prefix [OK]
Hint: Focus on words starting with prefix, not full word or sorting [OK]
Common Mistakes:
  • Confusing prefix with substring anywhere in word
  • Counting total characters instead of words
  • Sorting words instead of counting
2. Which of the following Rust code snippets correctly checks if a word starts with a given prefix prefix?
easy
A. word.starts_with(prefix)
B. word.contains(prefix)
C. word.ends_with(prefix)
D. word == prefix

Solution

  1. Step 1: Recall Rust string methods

    Rust's starts_with method checks if a string begins with a given substring.
  2. Step 2: Compare options

    contains checks anywhere, ends_with checks suffix, and == checks full equality, so only starts_with fits.
  3. Final Answer:

    word.starts_with(prefix) -> Option A
  4. Quick Check:

    Use starts_with() to check prefix [OK]
Hint: Use starts_with() method to check prefix in Rust strings [OK]
Common Mistakes:
  • Using contains() which matches anywhere, not just prefix
  • Using ends_with() which checks suffix
  • Comparing full equality instead of prefix
3. What is the output of this Rust code?
let words = vec!["apple", "app", "application", "banana"];
let prefix = "app";
let count = words.iter().filter(|w| w.starts_with(prefix)).count();
println!("{}", count);
medium
A. 3
B. 2
C. 1
D. 4

Solution

  1. Step 1: Identify words starting with "app"

    Words are "apple", "app", "application", and "banana". The first three start with "app".
  2. Step 2: Count matching words

    Count is 3 because "apple", "app", and "application" start with "app".
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    3 words start with "app" [OK]
Hint: Count words where starts_with(prefix) is true [OK]
Common Mistakes:
  • Counting only exact matches
  • Including words that don't start with prefix
  • Off-by-one counting errors
4. The following Rust code tries to count words starting with a prefix but gives wrong output. What is the bug?
let words = vec!["cat", "car", "dog", "scare"];
let prefix = "car";
let count = words.iter().filter(|w| w.contains(prefix)).count();
println!("{}", count);
medium
A. Missing semicolon after count declaration
B. Prefix variable is not declared correctly
C. The vector words is empty
D. Using contains() instead of starts_with() causes wrong matches

Solution

  1. Step 1: Understand difference between contains() and starts_with()

    contains() checks if prefix appears anywhere, not just at start.
  2. Step 2: Identify incorrect matches

    "scare" contains "car" but does not start with "car", so contains() wrongly counts it.
  3. Final Answer:

    Using contains() instead of starts_with() causes wrong matches -> Option D
  4. Quick Check:

    Use starts_with() to count prefix matches [OK]
Hint: Replace contains() with starts_with() to fix prefix counting [OK]
Common Mistakes:
  • Confusing contains() with starts_with()
  • Assuming contains() only matches prefix
  • Ignoring method differences
5. Given a list of words ["test", "tester", "testing", "toast", "team"] and prefix "te", which Rust code correctly counts words starting with the prefix and ignores case sensitivity?
hard
A. words.iter().filter(|w| w.contains(prefix)).count()
B. words.iter().filter(|w| w.starts_with(prefix)).count()
C. words.iter().filter(|w| w.to_lowercase().starts_with(&prefix.to_lowercase())).count()
D. words.iter().filter(|w| w.ends_with(prefix)).count()

Solution

  1. Step 1: Understand case sensitivity in Rust strings

    Rust's starts_with is case sensitive, so we convert both word and prefix to lowercase.
  2. 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.
  3. Final Answer:

    words.iter().filter(|w| w.to_lowercase().starts_with(&prefix.to_lowercase())).count() -> Option C
  4. Quick Check:

    Convert both strings to lowercase before starts_with() [OK]
Hint: Lowercase both word and prefix before starts_with() for case-insensitive [OK]
Common Mistakes:
  • Ignoring case sensitivity in starts_with()
  • Using contains() which is wrong for prefix
  • Using ends_with() instead of starts_with()
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard