-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathDecryptStringFromAlphabetToIntegerMapping.java
More file actions
36 lines (32 loc) · 1.11 KB
/
DecryptStringFromAlphabetToIntegerMapping.java
File metadata and controls
36 lines (32 loc) · 1.11 KB
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
import java.util.LinkedList;
import java.util.Queue;
public class DecryptStringFromAlphabetToIntegerMapping {
public String freqAlphabets(String s) {
final StringBuilder result = new StringBuilder();
final Queue<Character> queue = new LinkedList<>();
for (int index = 0 ; index < s.length() ; index++) {
if (queue.size() == 2) {
if (s.charAt(index) == '#') {
result.append(numberToChar(queue.poll(), queue.poll()));
continue;
} else {
result.append(numberToChar(queue.poll()));
}
}
queue.add(s.charAt(index));
}
while (!queue.isEmpty()) {
result.append(numberToChar(queue.poll()));
}
return result.toString();
}
private char numberToChar(char a, char b) {
return numberToChar(10 * (a - '0') + (b - '0'));
}
private char numberToChar(char digit) {
return numberToChar(digit - '0');
}
private char numberToChar(int digit) {
return (char) (digit + 'a' - 1);
}
}