Encrypt and Decrypt Strings LeetCode solution in Java

1
class Encrypter {
2

3
Map<String, Integer> encryptedDictCount;
4
int[] keys;
5
Set<String> dictionary;
6
String[] val;
7

8
public Encrypter(char[] keys, String[] values, String[] dictionary) {
9
this.keys = new int[26];
10
encryptedDictCount = new HashMap<>();
11
this.val = values.clone();
12
this.dictionary = new HashSet<>(Arrays.asList(dictionary));
13

14
for (int i = 0; i < keys.length; i++) {
15
this.keys[keys[i] - 'a'] = i;
16
}
17

18
for (String dict : dictionary) {
19
String encrpted = encrypt(dict);
20
encryptedDictCount.put(encrpted, encryptedDictCount.getOrDefault(encrpted, 0) + 1);
21
}
22
}
23

24
public String encrypt(String word1) {
25
StringBuilder sb = new StringBuilder();
26
for (int i = 0; i < word1.length(); i++) {
27
int c = word1.charAt(i) - 'a';
28
sb.append(val[keys[c]]);
29
}
30
return sb.toString();
31
}
32

33
public int decrypt(String word2) {
34
return encryptedDictCount.getOrDefault(word2, 0);
35
}
36
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0