A search box maintains a growing collection of words. You must process q operations in order. Each operation is one of:
ADD w - insert the word w into the collection (the same word may be added more than once; that has no extra effect).EXACT w - answer whether w has been added as a complete word so far.PREFIX p - answer whether at least one added word has p as a prefix (a word is a prefix of itself).Every word and prefix consists of lowercase English letters only.
Line 1: an integer q, the number of operations.
Each of the next q lines: one operation, either ADD w, EXACT w, or PREFIX p.
For each EXACT and PREFIX operation, in order, print a line containing YES or NO.
Example 1
Input
5 ADD apple ADD app EXACT app PREFIX ap EXACT ban
Expected
YES YES NO
Explanation
app was added, so EXACT app is YES. Both apple and app start with ap, so PREFIX ap is YES. ban was never added, so EXACT ban is NO.
Example 2
Input
3 ADD dog PREFIX do PREFIX da
Expected
YES NO
Explanation
dog starts with do, so PREFIX do is YES. No added word starts with da, so PREFIX da is NO.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →