0
0
DSA Typescriptprogramming~10 mins

Count Words with Given Prefix in DSA Typescript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Count Words with Given Prefix
Start with empty Trie
Insert each word
For each character in word
Create node if missing
Increment prefix count
After all words inserted
Search prefix
Traverse nodes for prefix chars
If prefix node found
Return count stored at prefix node
Else return 0
Build a Trie by inserting words, counting prefixes at each node. Then traverse prefix nodes to get count.
Execution Sample
DSA Typescript
class TrieNode {
  children: Map<string, TrieNode> = new Map();
  count: number = 0;
}

// Insert words and count prefixes
// Query prefix count
Insert words into Trie, increment counts for prefixes, then query prefix count.
Execution Table
StepOperationWord/PrefixNode CreatedPrefix Count UpdatedVisual State
1Insert"apple"a, p, p, l, e nodes createda:1, p:1, p:1, l:1, e:1root -> a(1) -> p(1) -> p(1) -> l(1) -> e(1)
2Insert"app"No new nodesa:2, p:2, p:2root -> a(2) -> p(2) -> p(2) -> l(1) -> e(1)
3Insert"ape"e node created under pa:3, p:3, e:1root -> a(3) -> p(3) -> p(2) -> l(1) -> e(1) -> e(1)
4Query"ap"NoNoCount at node 'p' after 'a' is 3
5Query"app"NoNoCount at node 'p' after 'ap' is 2
6Query"apple"NoNoCount at node 'e' after 'appl' is 1
7Query"bat"NoNoPrefix not found, return 0
💡 All words inserted and prefix queries completed.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6After Step 7
Trie Rootemptya node with count 1a node with count 2a node with count 3unchangedunchangedunchangedunchanged
Node 'a' count01233333
Node 'p' count (first p)01233333
Node 'p' count (second p)01222222
Node 'l' count01111111
Node 'e' count (under l)01111111
Node 'e' count (under p)00011111
Key Moments - 3 Insights
Why do we increment the count at each node when inserting a word?
Because each node represents a prefix. Incrementing count tracks how many words share that prefix, as shown in steps 1-3 in the execution_table.
What happens if the prefix does not exist in the Trie during query?
We return 0 immediately because no words start with that prefix, as shown in step 7 where 'bat' is not found.
Why do some nodes get created and others don't during insertion?
Nodes are created only if the character path does not exist yet. For example, in step 2 inserting 'app' no new nodes are created because 'a' and 'p' nodes already exist.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What is the prefix count at node 'a' after inserting "ape"?
A3
B2
C1
D0
💡 Hint
Check the 'Prefix Count Updated' column at step 3 for node 'a'.
At which step does the prefix count at the second 'p' node become 2?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the variable_tracker row for 'Node p count (second p)' after each step.
If we query prefix "appl", what would be the count returned based on the execution_table?
A3
B2
C1
D0
💡 Hint
Check the count at node 'l' after 'app' in the visual state and prefix counts.
Concept Snapshot
Count Words with Given Prefix using Trie:
- Insert words char by char
- Create nodes if missing
- Increment count at each node for prefix tracking
- Query prefix by traversing nodes
- Return count at last prefix node or 0 if missing
Full Transcript
We build a Trie data structure to count how many words start with a given prefix. Each node in the Trie represents a character and stores how many words pass through it as a prefix. When inserting words like 'apple', 'app', and 'ape', we create nodes for characters if they don't exist and increment counts at each node. For example, after inserting 'apple', the nodes for 'a', 'p', 'p', 'l', 'e' each have count 1. Inserting 'app' increments counts on 'a', 'p', 'p' to 2. Inserting 'ape' increments counts on 'a', 'p', and creates a new 'e' node under 'p'. When querying a prefix like 'ap', we traverse nodes for 'a' and 'p' and return the count stored at 'p', which is 3, meaning 3 words start with 'ap'. If the prefix is not found, like 'bat', we return 0. This method efficiently counts words sharing prefixes by storing counts during insertion.