Implement Trie

Hard
0/120
Average time to solve is 41m
227 upvotes
Asked in companies
MathworksSalesforceSamsung

Problem statement

Implement Trie Data Structure to support these operations:

insert(word) - To insert a string "word" in Trie
search(word) - To check if string "word" is present in Trie or not.
startsWith(word) - To check if there is any string in the Trie that starts with the given prefix string "word".


Three type of queries denote these operations:

Type 1: To insert a string "word" in Trie.
1 word

Type 2: To check if the string "word" is present in Trie or not.
2 word

Type 3: To check if there is any string in the Trie that starts with the given prefix string "word".
3 word


Detailed explanation ( Input/output format, Notes, Images )
Input format :
The first line contains an Integer 'Q' which denotes the number of queries to be run. Then next 'Q' lines denote each query:

The first and only line of each query contains the type of query and a string "word" separated by a single space.
Output format :
For each query of Type 2 print the string "true" if string "word" is present in Trie or "false" otherwise.
For each query of Type 3 print the string "true" if there is any string in the Trie that starts with the given prefix string "word" or "false" otherwise.

Output for every query will be printed in a separate line.
Note:
You are not required to print the output explicitly, it has already been taken care of. Just implement the function.
Constraints :
1 <= Q <= 5*10^4
1 <= W <= 10

Where 'Q' is the number of queries, and 'W' is the length of the "word".
All input of "word" will consist of only lowercase letters a-z.
Sample Input 1 :
5
1 hello
1 help
2 help
3 hel
2 hel 
Sample Output 1 :
true
true
false
 Explanation to Sample Input 1 :
Query 1: "hello" is inserted
Query 2: "help" is inserted
Query 3: "true" is printed as "help" is present
Query 4: "true" is printed as "hello" and "help" is present having the prefix "hel"
Query 5: "false" is printed as "hel" is not present
Sample Input 2 :
10
1 aaaa
1 aaaaaa
1 bcd
2 aaaaa
3 aaaaa
3 bc
2 bc
1 bc
3 bcda
2 bc
Sample Output 2 :
false
true
true
false
false
true
 Explanation to Sample Input 2 :
Query 1: "aaaa" is inserted
Query 2: "aaaaaa" is inserted
Query 3: "bcd" is inserted
Query 4: "false" is printed as "aaaaa" is not present
Query 5: "true" is printed as "aaaaaa" is present having the prefix "aaaaa"
Query 6: "true" is printed as "bcd" is present having the prefix "bc"
Query 7: "false" is printed as "bc" is not present
Query 8: "bc" is inserted
Query 9: "false" is printed as no word is present having the prefix "bcda"
Query 10: "true" is printed as "bc" is present
Hint

Try to make a data structure similar to a tree which can support required operations.

 

Approaches (2)
Hashmap/Dictionary Approach

Firstly we have defined the node class of Trie having members:

  • child  - storing the address of child nodes (hashmap of character number and address of Trie Node)
  • isEnd - a bool variable for marking this node as an end of some word.

Then we have defined our Trie Class having members:

  • root - The root node for whole Trie, every word starts from this node.
  • insert(word) - To insert a string "word" in Trie
  • search(word) - To check if string "word" is present in Trie or not.
  • startsWith(word) - To check if there is any string in the Trie that starts with the given prefix string "word".

Definition of insert(word):

  • Initialize the current node with the root node of Trie.
  • Iterate over the word from left to right and if there is no node present for the next character of the word then create a new node and store it in child member of the previous character’s node.
  • Keep updating the current node to the corresponding node for the next character of the word.
  • Finally, when we reached the end of the word, mark the isEnd member of the current node to true as it will denote the word is present or not.

Definition of search(word):

  • Initialize the current node with the root node of Trie.
  • Iterate over the word from left to right and if there is no node present for the next character of the word then return false as the word is not present in the Trie.
  • Keep updating the current node to the corresponding node for the next character of the word.
  • Finally, when we reached the end of the word, return the isEnd bool variable of the current node as it will denote the word is present or not.

Definition of startsWith(word):

  • Initialize the current node with the root node of Trie.
  • Iterate over the word from left to right and if there is no node present for the next character of the word then return false as the no word is present in the Trie with this word as a Prefix.
  • Keep updating the current node to the corresponding node for the next character of the word.
  • Finally, when we reached the end of the word, return true as some word must be present in the Trie as we are able to cover the whole prefix word.
Time Complexity

O(N*W) (insert - O(W), search - O(W), startsWith - O(W))

where N is the number of queries and W is the average length of words.

For all the operations we are iterating over the word and checking in hashmap for the next character of the word so that’s an O(1) operation on average so Overall Time Complexity for insert, search, startsWith is O(W).

Space Complexity

O(N*W) where N is the number of words inserted and W is the average length of words.

As we are making nodes of each character of a node so at max we can have all unique sequence of the words. This hashmap approach of storing child is more space-efficient than an array for storing child as we will only store the address for the nodes present in the Trie.

Video Solution
Unlock at level 3
(75% EXP penalty)
Code Solution
(100% EXP penalty)
Implement Trie
Full screen
Console