📘
Implement Trie (Prefix Tree)
MediumTries
Implement a Trie that supports insert, search, and startsWith operations.
Input: Line 1: comma-separated operations (insert/search/startsWith). Line 2: comma-separated arguments. Output: Result of each search/startsWith operation, one per line.
Example:
Input:
insert,search,search,startsWith,insert,search apple,apple,app,app,app,app
Output:
True False True True
Reasoning:
- The input operations are
insert, search, search, startsWith, insert, searchand the arguments areapple, apple, app, app, app, app. - The Trie is initially empty, then
appleis inserted, allowing the firstsearchforappleto returnTrue. - The next
searchforappreturnsFalsebecauseappis not a complete word in the Trie, andstartsWithforappreturnsTruebecauseappis a prefix ofapple. - After inserting
appinto the Trie, the finalsearchforappreturnsTrue.
Constraints:
- 1 <= word.length, prefix.length <= 2000
- Words consist of lowercase English letters
- At most 3 * 10^4 calls total
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.