MediumPro challengePythonJavaScriptTypeScript
Trie (Prefix Tree)
Data StructuresStrings
Implement a Trie class:
insert(word)— add a word.search(word)— returntrueif the exact word exists.startsWith(prefix)— returntrueif any word starts withprefix.
solve(ops) replays operations and returns results for search/startsWith.
Sample tests
Test #1Search in empty trie
Input: [[["search","hello"]]]
Output: [false]
Test #2Classic trie sequence from LeetCode 208
Input: [[["insert","apple"],["search","apple"],["search","app"],["startsWith","app"],["insert","app"],["search","app"]]]
Output: [null,true,false,true,null,true]
Test #3Prefix checks
Input: [[["insert","hi"],["startsWith","h"],["startsWith","hi"],["startsWith","hit"]]]
Output: [null,true,true,false]
Test #4Shorter word is a prefix of longer word
Input: [[["insert","a"],["insert","ab"],["search","a"],["search","ab"],["search","abc"]]]
Output: [null,null,true,true,false]
Test #5Two words with shared prefix
Input: [[["insert","cat"],["insert","car"],["search","car"],["search","can"],["startsWith","ca"]]]
Output: [null,null,true,false,true]