InferenceAI

Enter at least two characters to search.

Start free

Prefix Tree Operations

mediumDesign25 min25 XP

Simulate a trie. Each operation is ["insert", word], ["search", word] (is the exact word present?) or ["prefix", text] (does any inserted word start with text?). Return the boolean results of the search and prefix operations, in order.

Examples

EXAMPLE WALKTHROUGHclassic

A guided view of the published example, not a trace of your code.

INPUT[[["insert","apple"],["search","apple"],["search","app"],["prefix","app"],["insert","app"],["search","app"]]]
TASKprefix_tree_operations()
EXPECTED OUTPUT[true,false,true,true]

Start with the input values shown in this example.

Example 1 of 3

Input: operations = [["insert","apple"],["search","apple"],["search","app"],["prefix","app"],["insert","app"],["search","app"]]

Output: [true,false,true,true]

Explanation: classic

Input: operations = [["prefix","a"]]

Output: [false]

Explanation: empty trie

Input: operations = [["insert",""],["search",""],["prefix",""]]

Output: [true,true]

Explanation: empty word

Hint 1

Each node is a dictionary from character to child, plus an end-of-word flag.

Topics

DSADesignPython