Skip to content

Commit 2ab9555

Browse files
authored
Update Reconstruct Original Digits from English.java
1 parent 9e1c92a commit 2ab9555

File tree

1 file changed

+27
-28
lines changed

1 file changed

+27
-28
lines changed
Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
11
class Solution {
2-
public String originalDigits(String s) {
3-
int[] count = new int[26];
4-
for (char c : s.toCharArray()) {
5-
count[c - 'a']++;
6-
}
7-
8-
int[] out = new int[10];
9-
10-
out[0] = count['z' - 'a'];
11-
out[2] = count['w' - 'a'];
12-
out[4] = count['u' - 'a'];
13-
out[6] = count['x' - 'a'];
14-
out[8] = count['g' - 'a'];
15-
out[3] = count['h' - 'a'] - out[8];
16-
out[5] = count['f' - 'a'] - out[4];
17-
out[7] = count['s' - 'a'] - out[6];
18-
out[9] = count['i' - 'a'] - out[5] - out[6] - out[8];
19-
out[1] = count['n' - 'a'] - out[7] - 2 * out[9];
20-
21-
StringBuilder sb = new StringBuilder();
22-
23-
for (int i = 0; i < 10; i++) {
24-
for (int j = 0; j < out[i]; j++) {
25-
sb.append(i);
26-
}
27-
}
28-
29-
return sb.toString();
2+
public String originalDigits(String s) {
3+
int[] count = new int[10];
4+
for (int i = 0; i < s.length(); i++){
5+
char c = s.charAt(i);
6+
if (c == 'z') count[0]++;
7+
if (c == 'w') count[2]++;
8+
if (c == 'x') count[6]++;
9+
if (c == 's') count[7]++; //7-6
10+
if (c == 'g') count[8]++;
11+
if (c == 'u') count[4]++;
12+
if (c == 'f') count[5]++; //5-4
13+
if (c == 'h') count[3]++; //3-8
14+
if (c == 'i') count[9]++; //9-8-5-6
15+
if (c == 'o') count[1]++; //1-0-2-4
3016
}
17+
count[7] -= count[6];
18+
count[5] -= count[4];
19+
count[3] -= count[8];
20+
count[9] = count[9] - count[8] - count[5] - count[6];
21+
count[1] = count[1] - count[0] - count[2] - count[4];
22+
StringBuilder sb = new StringBuilder();
23+
for (int i = 0; i <= 9; i++){
24+
for (int j = 0; j < count[i]; j++){
25+
sb.append(i);
26+
}
27+
}
28+
return sb.toString();
29+
}
3130
}

0 commit comments

Comments
 (0)