-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.java
35 lines (30 loc) · 1002 Bytes
/
solution.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
class Encrypter {
String[] emap = new String[26];
HashMap<String, Integer> dmap = new HashMap<>();
public Encrypter(char[] keys, String[] values, String[] dictionary) {
Arrays.fill(emap, "|");
for(int i=0; i < keys.length; i++){
emap[keys[i] - 'a'] = values[i];
}
for(String dic : dictionary){
String enc = encrypt(dic);
dmap.put(enc, dmap.getOrDefault(enc, 0) + 1);
}
}
public String encrypt(String word1) {
StringBuilder sb = new StringBuilder();
for(char c : word1.toCharArray()){
sb.append(emap[c - 'a']);
}
return sb.toString();
}
public int decrypt(String word2) {
return dmap.getOrDefault(word2, 0);
}
}
/**
* Your Encrypter object will be instantiated and called as such:
* Encrypter obj = new Encrypter(keys, values, dictionary);
* String param_1 = obj.encrypt(word1);
* int param_2 = obj.decrypt(word2);
*/