-
Notifications
You must be signed in to change notification settings - Fork 5
/
PhoneDirectory.java
78 lines (70 loc) · 2.11 KB
/
PhoneDirectory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*https://practice.geeksforgeeks.org/problems/phone-directory4628/1*/
//store the words in each node
class TrieNode
{
HashMap<Character,TrieNode> map;
boolean isEndOfWord;
HashSet<String> wordsBelow;
TrieNode()
{
map = new HashMap<Character,TrieNode>();
isEndOfWord = false;
wordsBelow = new HashSet<String>();
}
}
class Trie
{
public TrieNode root;
Trie()
{
root = new TrieNode();
}
public void insert(String word)
{
TrieNode temp = root;
for (int i = 0; i < word.length(); ++i)
{
if (!temp.map.containsKey(word.charAt(i)))
temp.map.put(word.charAt(i),new TrieNode());
temp.wordsBelow.add(word);
temp = (TrieNode)temp.map.get(word.charAt(i));
}
temp.wordsBelow.add(word);
temp.isEndOfWord = true;
}
public ArrayList<String> getListOfWords(String query)
{
TrieNode temp = root;
//move till the end of the query string
for (int i = 0; i < query.length(); ++i)
{
temp = (TrieNode)temp.map.get(query.charAt(i));
if (temp == null) break; //break if temp becomes null midway
}
//if temp is null return 0
if (temp == null)
{
ArrayList<String> emptyList = new ArrayList<String>();
emptyList.add("0");
return emptyList;
}
//otherwise sort the list on that node and return
ArrayList<String> returnList = new ArrayList<>(temp.wordsBelow);
Collections.sort(returnList);
return returnList;
}
}
class Solution
{
static ArrayList<ArrayList<String>> displayContacts(int n, String contact[], String s)
{
ArrayList<ArrayList<String>> result = new ArrayList<ArrayList<String>>();
//add the contacts to trie
Trie trie = new Trie();
for (String str : contact) trie.insert(str);
//add the lists and return
for (int i = 1; i <= s.length(); ++i)
result.add(trie.getListOfWords(s.substring(0,i)));
return result;
}
}