v9.1.1
Menu

Implement Trie (Prefix Tree)

mediumFREETriesBlind 75

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, search and the arguments are apple, apple, app, app, app, app.
  • The Trie is initially empty, then apple is inserted, allowing the first search for apple to return True.
  • The next search for app returns False because app is not a complete word in the Trie, and startsWith for app returns True because app is a prefix of apple.
  • After inserting app into the Trie, the final search for app returns True.

Constraints

  • 1 <= word.length, prefix.length <= 2000
  • Words consist of lowercase English letters
  • At most 3 * 10^4 calls total

Learn this in

More from Blind 75

solution.py

Test results

Sign in to run your code against the test cases.