-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoRecommendation.java
57 lines (52 loc) · 1.5 KB
/
AutoRecommendation.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
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
class Trie{
public HashMap<Character,Trie> chars;
public List<Integer> nodes;
Trie(){
this.chars=new HashMap<>();
this.nodes=new ArrayList<>();
}
}
class AutoRecommendation {
Trie root=null;
AutoRecommendation(){
this.root=new Trie();
}
//insert word into trie
public void insertString(String word,int end){
Trie temp=this.root;
for(char character:word.toCharArray()){
if(temp.chars.getOrDefault(character,null)==null){
temp.chars.put(character,new Trie());
}
temp=temp.chars.get(character);
}
temp.nodes.add(end);
}
//search for the word in trie
public List<Integer> search(String word){
Trie temp=this.root;
for(char character:word.toCharArray()){
if(temp.chars.getOrDefault(character,null)==null){
temp.chars.put(character,new Trie());
}
temp=temp.chars.get(character);
}
return temp.nodes;
}
//
public List<String> getRecommendation(String word){
List<String> res=new ArrayList<>();
Trie temp=root;
for(char character:word.toCharArray()){
if(temp.chars.getOrDefault(character,null)==null){
temp.chars.put(character,new Trie());
}
temp=temp.chars.get(character);
}
res.add(word);
return null;
}
}