What if you could find all words starting with a few letters instantly, no matter how big your list is?
Why Count Words with Given Prefix in DSA Typescript?
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 all red apples in a giant basket by picking each apple up and checking its color.
Checking every word manually takes a lot of time, especially if the list is very long.
It's easy to make mistakes or miss some words when doing this by hand or with simple loops.
This slow and error-prone method makes it hard to get quick answers.
Using a special structure called a Trie, we can organize words so that all words with the same prefix share the same path.
This lets us quickly jump to the part where the prefix ends and count all words that start with it without checking each word individually.
let count = 0; for (let word of words) { if (word.startsWith(prefix)) { count++; } }
class TrieNode { children = new Map<string, TrieNode>(); count = 0; } // Insert words and count prefix matches efficiently
This lets us instantly find how many words start with any prefix, even in huge word lists, making searches fast and reliable.
Search engines use this to suggest words or phrases as you type, by quickly counting how many words start with the letters you entered.
Manual checking is slow and error-prone for large word lists.
Tries organize words by prefixes to speed up counting.
Counting words with a prefix becomes fast and easy.
Practice
countWordsStartingWith(words: string[], prefix: string): number do in the context of counting words with a given prefix?Solution
Step 1: Understand the function purpose
The function is designed to count words that begin with a specific prefix, not just contain it anywhere.Step 2: Compare options with function goal
Only Counts how many words in the list start with the given prefix matches the goal of counting words starting exactly with the prefix.Final Answer:
Counts how many words in the list start with the given prefix -> Option CQuick Check:
Counting words starting with prefix = Counts how many words in the list start with the given prefix [OK]
- Confusing 'start with' and 'contain' prefix
- Thinking it returns words instead of count
- Assuming it counts characters, not words
pre in an array words?Solution
Step 1: Identify correct method to filter words
Thefiltermethod returns an array of words starting with the prefix, thenlengthcounts them.Step 2: Check other options for correctness
words.map(word => word.startsWith(pre)).length returns an array of booleans, so length is total words, not count of matches. words.reduce((count, word) => count + word.includes(pre), 0) usesincludesincorrectly and sums booleans without conversion. words.forEach(word => word.startsWith(pre)) does not return a count.Final Answer:
words.filter(word => word.startsWith(pre)).length -> Option AQuick Check:
Filter + length counts matches = words.filter(word => word.startsWith(pre)).length [OK]
- Using map instead of filter for counting
- Using includes instead of startsWith
- Using forEach without counting
const words = ['apple', 'app', 'application', 'banana', 'apply']; const prefix = 'app'; const count = words.filter(word => word.startsWith(prefix)).length; console.log(count);
Solution
Step 1: Identify words starting with 'app'
Words starting with 'app' are 'apple', 'app', 'application', and 'apply' (4 words).Step 2: Count these words
Counting them gives 4.Final Answer:
4 -> Option BQuick Check:
Words starting with 'app' = 4 [OK]
- Counting 'banana' mistakenly
- Counting only exact 'app' word
- Confusing includes with startsWith
pre, but it returns 0. What is the error?const words = ['prefix', 'preach', 'prevent', 'post'];
const pre = 'pre';
let count = 0;
for (let i = 0; i < words.length; i++) {
if (words[i].startWith(pre)) {
count++;
}
}
console.log(count);Solution
Step 1: Check method name correctness
The method to check prefix isstartsWith, but code usesstartWith, which is undefined.Step 2: Understand effect of typo
Because of the typo, condition is always false, so count remains 0.Final Answer:
Method name should be startsWith, not startWith -> Option AQuick Check:
Correct method name fixes count [OK]
- Using startWith instead of startsWith
- Incorrect loop index start
- Wrong count initialization
Solution
Step 1: Understand prefix query optimization
Trie stores words by characters in a tree, allowing fast prefix searches.Step 2: Compare other data structures
Sorted arrays require binary search but less efficient for many prefix queries. Hash maps store full words, not prefixes. Stacks do not support prefix queries efficiently.Final Answer:
Trie (Prefix Tree) -> Option DQuick Check:
Trie enables fast prefix counts [OK]
- Choosing array or hash map for prefix queries
- Not knowing Trie supports prefix efficiently
- Confusing stack usage for prefix search
Ready to prove it?
Pass the test and this pattern moves unknown
