All problems

Problem 687

Implement Trie (Prefix Tree)

Medium · 20 pts · Design, Trie, String

Problem

Implement a prefix tree that stores words. Process Q operations: 'insert w' adds the word w; 'search w' prints true if w was inserted before (as a whole word), otherwise false; 'startsWith p' prints true if some inserted word starts with p, otherwise false. Each trie node keeps a map from letters to children and a flag marking the end of a word, so each operation takes time proportional to the word length.

Input

The first line contains Q. Each of the next Q lines contains one operation. Words use lowercase letters. There is at least one search or startsWith.

Output

Print true or false for every search and startsWith, one per line.

Constraints

1 <= Q <= 30000; 1 <= |w|, |p| <= 2000

Example input

6
insert apple
search apple
search app
startsWith app
insert app
search app

Example output

true
false
true
true

Explanation

After insert apple, search apple is true, search app is false (app was not inserted as a word) and startsWith app is true. After insert app, search app becomes true.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Seat Reservation Manager Next: The Skyline Problem