-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMapSumPairs.java
141 lines (127 loc) · 3.26 KB
/
MapSumPairs.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*https://leetcode.com/problems/map-sum-pairs/*/
class TrieNode
{
TrieNode[] hash;
boolean isEnd;
TrieNode()
{
hash = new TrieNode[26];
isEnd = false;
}
}
class Trie
{
TrieNode trieRoot;
Trie()
{
trieRoot = new TrieNode();
}
public void insert(String word)
{
TrieNode temp = trieRoot;
for (char ch : word.toCharArray())
{
if (temp.hash[ch-'a'] == null)
temp.hash[ch-'a'] = new TrieNode();
temp = temp.hash[ch-'a'];
}
temp.isEnd = true;
}
public List<String> getListOfWords(String pre)
{
List<String> wordsList = new ArrayList<String>();
TrieNode temp = trieRoot;
for (char ch : pre.toCharArray())
{
if (temp == null) return wordsList;
temp = temp.hash[ch-'a'];
}
dfs(wordsList,temp,new StringBuilder(pre));
return wordsList;
}
private void dfs(List<String> list, TrieNode root, StringBuilder word)
{
if (root == null) return;
if (root.isEnd) list.add(word.toString());
for (int i = 0; i < 26; ++i)
{
if (root.hash[i] != null)
{
word.append(Character.toString((char)i+'a'));
dfs(list,root.hash[i],word);
word.delete(word.length()-1,word.length());
}
}
}
}
class MapSum {
Trie words;
HashMap<String,Integer> map;
public MapSum() {
map = new HashMap<String,Integer>();
words = new Trie();
}
public void insert(String key, int val) {
map.put(key,val);
words.insert(key);
}
public int sum(String prefix) {
List<String> wordsWithPrefix = words.getListOfWords(prefix);
int valueSum = 0;
for (String word : wordsWithPrefix)
valueSum += map.get(word);
return valueSum;
}
}
/**
* Your MapSum object will be instantiated and called as such:
* MapSum obj = new MapSum();
* obj.insert(key,val);
* int param_2 = obj.sum(prefix);
*/
class MapSum {
MapSum[] child;
int value;
public MapSum() {
child=new MapSum[26];
}
public void insert(String key, int val) {
MapSum trie=this;
for(int i=0;i<key.length();i++){
if(trie.child[key.charAt(i)-'a']==null){
trie.child[key.charAt(i)-'a']=new MapSum();
}
trie=trie.child[key.charAt(i)-'a'];
if(i==key.length()-1){
trie.value=val;
return;
}
}
}
public int sum(String prefix) {
MapSum trie=this;
for(int i=0;i<prefix.length();i++){
if(trie.child[prefix.charAt(i)-'a']==null){
return 0;
}
trie=trie.child[prefix.charAt(i)-'a'];
}
return sum(trie);
}
public int sum(MapSum trie){
int res=0;
res+=trie.value;
for(MapSum m: trie.child){
if(m!=null){
res+=sum(m);
}
}
return res;
}
}
/**
* Your MapSum object will be instantiated and called as such:
* MapSum obj = new MapSum();
* obj.insert(key,val);
* int param_2 = obj.sum(prefix);
*/