Implement a phone directory

Hard
0/120
Average time to solve is 50m
profile
Contributed by
110 upvotes
Asked in companies
AmazonMicrosoftDelhivery

Problem statement

You are given a list/array of strings which denotes the contacts that exist in your phone directory. The search query on a string ‘str’ which is a query string displays all the contacts which are present in the given directory with the prefix as ‘str’. One special property of the search function is that when a user searches for a contact from the contact list then suggestions (contacts with prefix as the string entered so for) are shown after the user enters each character.

Note :
If no suggestions are found, return an empty 2D array.
For Example :

alt text

In the above example everytime we enter a character, a few suggestions display the strings which contain the entered string as prefixes.
Detailed explanation ( Input/output format, Notes, Images )
Input format :
The first line contains a single integer ‘T’ denoting the number of test cases.

The first line of each test case contains a single integer ‘N’ denoting the number of elements in the array/list.

The second line contains ‘N’ single space-separated strings denoting the contacts of the given array/list or contact list.

The third line contains a single string denoting the query string.
Output format :
For each test case, for the given query string you need to print all the possible contacts or suggestions that are present in the given directory corresponding to each entered character in the given query string. 
If no suggestions are found then the message “No suggestions found” is printed.
Note :
You do not need to print anything; It has already been taken care of.
Constraints :
1 <= T <= 50
1 <= N <= 100
1 <= len <= 10
ARR[i] contains lowercase English alphabets.

All the given strings contain lowercase English alphabets.

Time Limit: 1 sec.
Sample Input 1 :
2
5
cod coding codding code coly
coding
2
ninjas coding
ninja
Sample Output 1 :
cod coding codding code coly
cod coding codding code coly
cod coding codding code coly
coding
coding
coding
ninjas
ninjas
ninjas
ninjas
ninjas
Explanation to Sample Input 1 :
In the first test case, 
The suggestions for “c” is {“cod”, “coding”, “codding”, “code”, “coly”}.
The suggestions for “co” is {“cod”, “coding”, “codding”, “code”, “coly”}.
The suggestions for “cod” is {“cod”, “coding”, “codding”, “code”, “coly”}.
The suggestion for “codi” is {“coding”}.
The suggestion for “codin” is {“coding”}.
The suggestion for “coding” is {“coding”}.

In the second test case, 
The suggestion for “n” is {“ninjas”}.
The suggestion for “ni” is {“ninjas”}.
The suggestion for “nin” is {“ninjas”}.
The suggestion for “ninj” is {“ninjas”}.
The suggestion for “ninja” is {“ninjas”}.
Sample Input 2 :
3
2
coding ninjas
cell
2
ab abc
a
2
ab abc
b
Sample Output 2 :
coding
ab abc
No suggestions found
Explanation to Sample Input 2 :
In the first test case, 
The suggestion for “c” is {“coding”}.
For the rest of the letters, there are no suggestions.

In the second test case,
The suggestion for “a” is {“ab”, “abc”}.

In the third test case, no suggestions are found.
Hint

Think of a data structure that can be used to search a string.

Approaches (2)
Trie + HashMap

A search query on a Trie is being used to determine whether the string is present or not in the trie, but in this case, we are asked to find all the strings with each prefix of ‘str’. This is equivalent to doing a DFS traversal on a graph. 

 

From a Trie node, we need to visit adjacent Trie nodes and do this recursively until there is no more adjacent. This recursive function will take 2 arguments one as Trie Node which points to the current Trie Node being visited and the other as the string which stores the string found so far with prefix as ‘str’. We are creating an unordered_map where each alphabet points to a trie node.

 

Each Trie Node stores a boolean variable “isEnd” which becomes true if the node represents the end of contact or element of the given contact list. It is nothing but a variable that signifies that we don’t need to move forward to do a search because we reached the end of the particular word of the contactList.

 

  • Our approach is to find the prefix starting with the string formed is to check if the prefix exists in the Trie, if yes then call the display function which will display all the corresponding suggestions.
  • After every entered character we check if the string exists in the Trie.
  • We do not need to check again and again, so to avoid that we can maintain a pointer PREVthat points to the TrieNode which corresponds to the last entered character by the user, now we need to check the child node for the “PREV” when the user enters another character to check if it exists in the Trie.
  • If the new prefix is not in the Trie, then all the strings which are formed by entering characters after the ‘prefix’ can’t be found in Trie too. So we break the loop because, for all the remaining characters, it’s not possible to find suggestions anymore.
Time Complexity

O(N * (W ^ 2)), where ‘N’ is the number of elements in the given array/list and ‘W’ is the average length of the string.

 

Consider an example when our word list will be like this :

{“a”,”aa”,”aaa”,”aaaa”,”aaaaa”,”aaaaaa”,”aaaaaa”} and we are looking for suggestions for the query string “aaaaaa”. 

So for inserting the given contact list into trie will take O(N*W) time. And secondly, in order to store the suggestions, we’ll include all the lists for every character of the given prefix. 

For example, Now considering all the W characters in the prefix and traversing all the N words which will take O(N*W), and combining both of these will result in O(W*(N*W)) time. So the overall time complexity will be O(N*(W^2)).

Space Complexity

O(N * W), where ‘N’ is the number of elements in the given array/list and ‘W’ is the average length of the string.

 

Since for each character in the prefix of the intermediate strings we are storing a map for its children where each alphabet points to a trie node resulting in taking the space for ‘W’ characters as O(N * W) space since we are looking for each string in the given word list. 

So the overall space complexity will be O(N * W).

Code Solution
(100% EXP penalty)
Implement a phone directory
Full screen
Console