Implement Trie

Design a trie that supports inserting words, searching exact words, and checking whether a prefix exists. Pattern focus: Trie Node Structure. Build a clean node model and keep the operations consistent across empty, duplicate, and prefix-heavy inputs.

Input Format

operations = trie commands, values = command arguments

Output Format

boolean results for each search or startsWith command in order

Constraints

  • 1 <= operations.length <= 10^5
  • 1 <= total characters across all words <= 10^6
  • Words contain only lowercase English letters.

Examples

Example 1:

Input:

operations = ["insert","search","search","startsWith","insert","search"]
values = ["apple","apple","app","app","app","app"]

Output:

[true,false,true,true]

Explanation:

Insert one word, verify an exact match, test a missing word, then add the prefix word and confirm it exists.

Example 2:

Input:

operations = []
values = []

Output:

[]

Explanation:

No trie commands produces no query results.

Example 3:

Input:

operations = ["insert","insert","search","startsWith"]
values = ["hello","hello","hell","he"]

Output:

[false,true]

Explanation:

Duplicate inserts should not break search or prefix lookup.

Loading...
Implement Trie - Trie DSA Problem