|
22 | 22 | */
|
23 | 23 | public class _423 {
|
24 | 24 |
|
25 |
| - public String originalDigits(String s) { |
26 |
| - /**we can use one char as a representative to uniquely stand for one number, |
27 |
| - * for some numbers that we cannot find a unique representive, we can dedup. |
28 |
| - * e.g. for number one, if we use 'o' as its representive, then 'o' also exists in numbers 2, 4 and 0, so |
29 |
| - * we need to dedupe the 'o' in those numbers. |
30 |
| - * Also, the order to dedupe matters: |
31 |
| - * we'll have to dedupe for counts[3], counts[5], counts[7] first before we dedupe counts[1] and counts[9].*/ |
32 |
| - int[] counts = new int[10]; |
33 |
| - for (int i = 0; i < s.length(); i++) { |
34 |
| - if (s.charAt(i) == 'o') { |
35 |
| - counts[1]++;//2,4,0 |
| 25 | + public static class Solution1 { |
| 26 | + public String originalDigits(String s) { |
| 27 | + /**we can use one char as a representative to uniquely stand for one number, |
| 28 | + * for some numbers that we cannot find a unique representive, we can dedup. |
| 29 | + * e.g. for number one, if we use 'o' as its representive, then 'o' also exists in numbers 2, 4 and 0, so |
| 30 | + * we need to dedupe the 'o' in those numbers. |
| 31 | + * Also, the order to dedupe matters: |
| 32 | + * we'll have to dedupe for counts[3], counts[5], counts[7] first before we dedupe counts[1] and counts[9].*/ |
| 33 | + int[] counts = new int[10]; |
| 34 | + for (int i = 0; i < s.length(); i++) { |
| 35 | + if (s.charAt(i) == 'o') { |
| 36 | + counts[1]++;//2,4,0 |
| 37 | + } |
| 38 | + if (s.charAt(i) == 'w') { |
| 39 | + counts[2]++; |
| 40 | + } |
| 41 | + if (s.charAt(i) == 'h') { |
| 42 | + counts[3]++;//8 |
| 43 | + } |
| 44 | + if (s.charAt(i) == 'u') { |
| 45 | + counts[4]++; |
| 46 | + } |
| 47 | + if (s.charAt(i) == 'f') { |
| 48 | + counts[5]++;//4 |
| 49 | + } |
| 50 | + if (s.charAt(i) == 'x') { |
| 51 | + counts[6]++; |
| 52 | + } |
| 53 | + if (s.charAt(i) == 'v') { |
| 54 | + counts[7]++;//5 |
| 55 | + } |
| 56 | + if (s.charAt(i) == 'g') { |
| 57 | + counts[8]++; |
| 58 | + } |
| 59 | + if (s.charAt(i) == 'i') { |
| 60 | + counts[9]++;//5,6,8 |
| 61 | + } |
| 62 | + if (s.charAt(i) == 'z') { |
| 63 | + counts[0]++; |
| 64 | + } |
36 | 65 | }
|
37 |
| - if (s.charAt(i) == 'w') { |
38 |
| - counts[2]++; |
39 |
| - } |
40 |
| - if (s.charAt(i) == 'h') { |
41 |
| - counts[3]++;//8 |
42 |
| - } |
43 |
| - if (s.charAt(i) == 'u') { |
44 |
| - counts[4]++; |
45 |
| - } |
46 |
| - if (s.charAt(i) == 'f') { |
47 |
| - counts[5]++;//4 |
48 |
| - } |
49 |
| - if (s.charAt(i) == 'x') { |
50 |
| - counts[6]++; |
51 |
| - } |
52 |
| - if (s.charAt(i) == 'v') { |
53 |
| - counts[7]++;//5 |
54 |
| - } |
55 |
| - if (s.charAt(i) == 'g') { |
56 |
| - counts[8]++; |
57 |
| - } |
58 |
| - if (s.charAt(i) == 'i') { |
59 |
| - counts[9]++;//5,6,8 |
60 |
| - } |
61 |
| - if (s.charAt(i) == 'z') { |
62 |
| - counts[0]++; |
63 |
| - } |
64 |
| - } |
65 | 66 |
|
66 |
| - counts[3] -= counts[8]; |
67 |
| - counts[5] -= counts[4]; |
68 |
| - counts[7] -= counts[5]; |
69 |
| - counts[1] = counts[1] - (counts[2] + counts[4] + counts[0]); |
70 |
| - counts[9] = counts[9] - (counts[5] + counts[6] + counts[8]); |
| 67 | + counts[3] -= counts[8]; |
| 68 | + counts[5] -= counts[4]; |
| 69 | + counts[7] -= counts[5]; |
| 70 | + counts[1] = counts[1] - (counts[2] + counts[4] + counts[0]); |
| 71 | + counts[9] = counts[9] - (counts[5] + counts[6] + counts[8]); |
71 | 72 |
|
72 |
| - StringBuilder stringBuilder = new StringBuilder(); |
73 |
| - for (int i = 0; i < 10; i++) { |
74 |
| - for (int j = 0; j < counts[i]; j++) { |
75 |
| - stringBuilder.append(i); |
| 73 | + StringBuilder stringBuilder = new StringBuilder(); |
| 74 | + for (int i = 0; i < 10; i++) { |
| 75 | + for (int j = 0; j < counts[i]; j++) { |
| 76 | + stringBuilder.append(i); |
| 77 | + } |
76 | 78 | }
|
| 79 | + return stringBuilder.toString(); |
77 | 80 | }
|
78 |
| - return stringBuilder.toString(); |
79 | 81 | }
|
80 | 82 |
|
81 | 83 | }
|
0 commit comments