Skip to content

Commit 7ee8d0d

Browse files
refactor 423
1 parent b9206a8 commit 7ee8d0d

File tree

2 files changed

+54
-55
lines changed

2 files changed

+54
-55
lines changed

src/main/java/com/fishercoder/solutions/_423.java

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -22,60 +22,62 @@
2222
*/
2323
public class _423 {
2424

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+
}
3665
}
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-
}
6566

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]);
7172

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+
}
7678
}
79+
return stringBuilder.toString();
7780
}
78-
return stringBuilder.toString();
7981
}
8082

8183
}

src/test/java/com/fishercoder/_423Test.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,22 @@
66

77
import static junit.framework.Assert.assertEquals;
88

9-
/**
10-
* Created by fishercoder on 4/27/17.
11-
*/
129
public class _423Test {
13-
private static _423 test;
10+
private static _423.Solution1 solution1;
1411
private static String expected;
1512
private static String actual;
1613
private static String s;
1714

1815
@BeforeClass
1916
public static void setup() {
20-
test = new _423();
17+
solution1 = new _423.Solution1();
2118
}
2219

2320
@Test
2421
public void test1() {
2522
s = "fviefuro";
2623
expected = "45";
27-
actual = test.originalDigits(s);
24+
actual = solution1.originalDigits(s);
2825
assertEquals(expected, actual);
2926
}
3027
}

0 commit comments

Comments
 (0)