Your free access ends in 7 days — and you haven’t tried it yet. Watch one algorithm run, start to finish. It takes about two minutes.

Try one problem
0
Concept

Implement Trie Methods

LeetCode #208Medium
insert · search · startsWith
step 1 / 9
trie
○ = node · ◎ ringed = end of a word
insert / search / startsWith
1insert(word):
2 cur = root
3 for ch in word: descend, creating if needed
4 cur.isWord = true
5
6search(word):
7 walk word; node = end
8 return node exists AND node.isWord
9
10startsWith(prefix):
11 walk prefix; return the path exists
state
  • methodsinsert · search · startsWith

line 1Implement the three Trie methods: insert(word), search(word) — is it a STORED word? — and startsWith(prefix) — does any word start this way? The subtle one is search vs startsWith.