-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution423.java
More file actions
67 lines (63 loc) · 1.85 KB
/
Copy pathsolution423.java
File metadata and controls
67 lines (63 loc) · 1.85 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
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
import java.util.HashMap;
import java.util.LinkedHashMap;
public class solution423 {
private final String[] StringForNum = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
HashMap<Character, Integer> map = new HashMap<>();
HashMap<String,Integer> map1 = new LinkedHashMap<>();
HashMap<String,Integer> map2 = new LinkedHashMap<>();
int[] num = new int[10];
public String originalDigits(String s) {
map1.put("eight",8);
map1.put("zero",0);
map1.put("two",2);
map1.put("six",6);
map1.put("four",4);
map2.put("seven",7);
map2.put("five",5);
map2.put("three",3);
map2.put("nine",9);
map2.put("one",1);
char[] chars = s.toCharArray();
for (char c : chars) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
for (String key:map1.keySet()) {
int i = map1.get(key);
while (isOk(key)) {
num[i]++;
}
}
for (String key:map2.keySet()) {
int i = map2.get(key);
while (isOk(key)) {
num[i]++;
}
}
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < 10; i++) {
while (num[i] > 0) {
stringBuilder.append(i);
num[i]--;
}
}
return stringBuilder.toString();
}
public boolean isOk(String s) {
char[] chars = s.toCharArray();
int sum = 0;
for (char c : chars) {
if (map.get(c) == null || map.get(c) == 0) {
return false;
}
sum++;
}
if(sum ==s.length())
{
for(char c : chars)
{
map.put(c,map.get(c)-1);
}
}
return true;
}
}