0
0
DSA Typescriptprogramming

Count Words with Given Prefix in DSA Typescript

Choose your learning style9 modes available
Mental Model
We want to find how many words start with a certain beginning part. We look at each word and check if it begins with that part.
Analogy: Imagine you have a box of books and you want to count how many titles start with the word 'The'. You look at each title and count if it starts with 'The'.
words: [apple, app, ape, bat, ball]
prefix: app

Check each word:
apple -> starts with app
app -> starts with app
ape -> does not
bat -> does not
ball -> does not
Dry Run Walkthrough
Input: words: ['apple', 'app', 'ape', 'bat', 'ball'], prefix: 'app'
Goal: Count how many words start with 'app'
Step 1: Check 'apple' if it starts with 'app'
apple (starts with 'app') -> count = 1
Why: We found one word starting with the prefix
Step 2: Check 'app' if it starts with 'app'
apple, app (starts with 'app') -> count = 2
Why: Another word matches the prefix
Step 3: Check 'ape' if it starts with 'app'
apple, app, ape (does not start with 'app') -> count = 2
Why: This word does not match the prefix, so count stays
Step 4: Check 'bat' if it starts with 'app'
apple, app, ape, bat (does not start with 'app') -> count = 2
Why: No match, count unchanged
Step 5: Check 'ball' if it starts with 'app'
apple, app, ape, bat, ball (does not start with 'app') -> count = 2
Why: No match, count unchanged
Result:
Final count = 2
Annotated Code
DSA Typescript
class WordPrefixCounter {
  words: string[];

  constructor(words: string[]) {
    this.words = words;
  }

  countWordsStartingWith(prefix: string): number {
    let count = 0;
    for (const word of this.words) {
      if (word.startsWith(prefix)) {
        count++;
      }
    }
    return count;
  }
}

// Driver code
const words = ['apple', 'app', 'ape', 'bat', 'ball'];
const prefix = 'app';
const counter = new WordPrefixCounter(words);
console.log(counter.countWordsStartingWith(prefix));
for (const word of this.words) {
iterate over each word in the list
if (word.startsWith(prefix)) {
check if current word begins with the prefix
count++;
increment count when prefix matches
OutputSuccess
2
Complexity Analysis
Time: O(n * m) because we check each of the n words and compare up to m characters of the prefix
Space: O(n) for storing the list of words
vs Alternative: A trie data structure can speed up prefix queries to O(m) after building, but building the trie costs O(n * m)
Edge Cases
empty words list
count returns 0 because no words to check
DSA Typescript
for (const word of this.words) {
empty prefix string
all words start with empty prefix, so count equals total words
DSA Typescript
if (word.startsWith(prefix)) {
no words start with prefix
count returns 0 as no matches found
DSA Typescript
if (word.startsWith(prefix)) {
When to Use This Pattern
When you need to count how many strings start with a certain beginning, use a simple loop with prefix check or a trie for faster repeated queries.
Common Mistakes
Mistake: Checking if prefix is inside the word anywhere instead of only at the start
Fix: Use word.startsWith(prefix) instead of word.includes(prefix)
Summary
Counts how many words in a list start with a given prefix.
Use when you want to find how many words share the same beginning letters.
The key is to check each word's start against the prefix exactly.