All Topics

Tries

Prefix trees for efficient string operations — search, autocomplete, and XOR tricks

0/32 solved

Insert / Search / StartsWith

Core trie operations for prefix-based string lookups

O(L) per operation where L = word length O(N * L) for N words

Approach

Build a tree where each node represents a character. To insert, traverse/create nodes character by character. To search, traverse nodes — return true only if the last node has isEnd=true. StartsWith is the same but doesn't check isEnd.

How to Recognize

1

Need efficient prefix matching across many strings

2

Problem involves autocomplete or dictionary lookup

3

Phrases like 'implement trie', 'prefix search', 'magic dictionary'

4

Multiple string queries on a fixed dictionary

Pro Tips

Each node has up to 26 children (for lowercase English) and an isEnd flag

Use HashMap children for large alphabets or Unicode

Trie search is O(L) where L is word length, regardless of dictionary size

8

Total

1

Easy

6

Medium

1

Hard

Time

O(L) per operation where L = word length

Space

O(N * L) for N words