Skip to content

Commit 3e05583

Browse files
refactor 408
1 parent 96025bc commit 3e05583

File tree

2 files changed

+173
-172
lines changed

2 files changed

+173
-172
lines changed
Lines changed: 58 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package com.fishercoder.solutions;
22

33
/**
4-
* Given a non-empty string s and an abbreviation abbr, return whether the string matches with the given abbreviation.
5-
6-
A string such as "word" contains only the following valid abbreviations:
4+
* 408. Valid Word Abbreviation
5+
*
6+
* Given a non-empty string s and an abbreviation abbr,
7+
* return whether the string matches with the given abbreviation.
8+
* A string such as "word" contains only the following valid abbreviations:
79
810
["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]
9-
Notice that only the above abbreviations are valid abbreviations of the string "word". Any other string is not a valid abbreviation of "word".
11+
Notice that only the above abbreviations are valid abbreviations of the string "word".
12+
Any other string is not a valid abbreviation of "word".
1013
1114
Note:
1215
Assume s contains only lowercase letters and abbr contains only lowercase letters and digits.
@@ -22,61 +25,65 @@
2225
*/
2326
public class _408 {
2427

25-
public boolean validWordAbbreviation(String word, String abbr) {
26-
if (abbr.length() > word.length()) {
27-
return false;
28-
} else {
29-
char[] abbrChars = abbr.toCharArray();
30-
char[] wordChars = word.toCharArray();
31-
if (abbr.length() == word.length()) {
32-
boolean prevDigit = false;
33-
for (int i = 0, j = 0; i < abbrChars.length && j < wordChars.length; i++, j++) {
34-
if (Character.isDigit(abbrChars[i]) && !prevDigit) {
35-
prevDigit = true;
36-
if (Character.getNumericValue(abbrChars[i]) != 1) {
28+
public static class Solution1 {
29+
public boolean validWordAbbreviation(String word, String abbr) {
30+
if (abbr.length() > word.length()) {
31+
return false;
32+
} else {
33+
char[] abbrChars = abbr.toCharArray();
34+
char[] wordChars = word.toCharArray();
35+
if (abbr.length() == word.length()) {
36+
boolean prevDigit = false;
37+
for (int i = 0, j = 0; i < abbrChars.length && j < wordChars.length; i++, j++) {
38+
if (Character.isDigit(abbrChars[i]) && !prevDigit) {
39+
prevDigit = true;
40+
if (Character.getNumericValue(abbrChars[i]) != 1) {
41+
return false;
42+
}
43+
} else if (Character.isDigit(abbrChars[i]) && prevDigit) {
3744
return false;
45+
} else if (abbrChars[i] != wordChars[j]) {
46+
return false;
47+
} else if (prevDigit) {
48+
prevDigit = false;
3849
}
39-
} else if (Character.isDigit(abbrChars[i]) && prevDigit) {
40-
return false;
41-
} else if (abbrChars[i] != wordChars[j]) {
42-
return false;
43-
} else if (prevDigit) {
44-
prevDigit = false;
4550
}
46-
}
47-
return true;
48-
} else {
49-
StringBuilder stringBuilder = new StringBuilder();
50-
boolean firstDigit = true;
51-
for (int i = 0, j = 0; i < abbrChars.length && j < wordChars.length; i++) {
52-
while (i < abbrChars.length && Character.isDigit(abbrChars[i])) {
53-
if (firstDigit && Character.getNumericValue(abbrChars[i]) == 0) {
51+
return true;
52+
} else {
53+
StringBuilder stringBuilder = new StringBuilder();
54+
boolean firstDigit = true;
55+
for (int i = 0, j = 0; i < abbrChars.length && j < wordChars.length; i++) {
56+
while (i < abbrChars.length && Character.isDigit(abbrChars[i])) {
57+
if (firstDigit && Character.getNumericValue(abbrChars[i]) == 0) {
58+
return false;
59+
}
60+
stringBuilder.append(abbrChars[i]);
61+
i++;
62+
firstDigit = false;
63+
}
64+
firstDigit = true;
65+
if (!stringBuilder.toString().isEmpty()) {
66+
int number = Integer.valueOf(stringBuilder.toString());
67+
j += number;
68+
stringBuilder.setLength(0);
69+
}
70+
if ((i >= abbrChars.length && j < wordChars.length) || (i < abbrChars.length
71+
&& j >= wordChars.length)) {
5472
return false;
5573
}
56-
stringBuilder.append(abbrChars[i]);
57-
i++;
58-
firstDigit = false;
59-
}
60-
firstDigit = true;
61-
if (!stringBuilder.toString().isEmpty()) {
62-
int number = Integer.valueOf(stringBuilder.toString());
63-
j += number;
64-
stringBuilder.setLength(0);
65-
}
66-
if ((i >= abbrChars.length && j < wordChars.length) || (i < abbrChars.length && j >= wordChars.length)) {
67-
return false;
68-
}
69-
if (i < abbrChars.length && j < wordChars.length && abbrChars[i] != wordChars[j]) {
70-
return false;
71-
}
72-
if (j > wordChars.length && i <= abbrChars.length) {
73-
return false;
74+
if (i < abbrChars.length
75+
&& j < wordChars.length
76+
&& abbrChars[i] != wordChars[j]) {
77+
return false;
78+
}
79+
if (j > wordChars.length && i <= abbrChars.length) {
80+
return false;
81+
}
82+
j++;
7483
}
75-
j++;
84+
return true;
7685
}
77-
return true;
7886
}
7987
}
8088
}
81-
8289
}

src/test/java/com/fishercoder/_408Test.java

Lines changed: 115 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -7,126 +7,120 @@
77

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

10-
/**
11-
* Created by fishercoder on 1/21/17.
12-
*/
1310
public class _408Test {
14-
private static _408 test;
15-
private static Boolean expected;
16-
private static Boolean actual;
17-
private static String word;
18-
private static String abbr;
19-
20-
@BeforeClass
21-
public static void setup() {
22-
test = new _408();
23-
}
24-
25-
@Before
26-
public void setupForEachTest() {
27-
word = "";
28-
abbr = "";
29-
}
30-
31-
@Test
32-
public void test1() {
33-
word = "internationalization";
34-
abbr = "i12iz4n";
35-
expected = true;
36-
actual = test.validWordAbbreviation(word, abbr);
37-
assertEquals(expected, actual);
38-
39-
}
40-
41-
@Test
42-
public void test2() {
43-
word = "apple";
44-
abbr = "a2e";
45-
expected = false;
46-
actual = test.validWordAbbreviation(word, abbr);
47-
assertEquals(expected, actual);
48-
49-
}
50-
51-
@Test
52-
public void test3() {
53-
word = "internationalization";
54-
abbr = "i5a11o1";
55-
expected = true;
56-
actual = test.validWordAbbreviation(word, abbr);
57-
assertEquals(expected, actual);
58-
59-
}
60-
61-
@Test
62-
public void test4() {
63-
word = "hi";
64-
abbr = "1";
65-
expected = false;
66-
actual = test.validWordAbbreviation(word, abbr);
67-
assertEquals(expected, actual);
68-
}
69-
70-
@Test
71-
public void test5() {
72-
word = "a";
73-
abbr = "1";
74-
expected = true;
75-
actual = test.validWordAbbreviation(word, abbr);
76-
assertEquals(expected, actual);
77-
}
78-
79-
@Test
80-
public void test6() {
81-
word = "a";
82-
abbr = "2";
83-
expected = false;
84-
actual = test.validWordAbbreviation(word, abbr);
85-
assertEquals(expected, actual);
86-
}
87-
88-
@Test
89-
public void test7() {
90-
word = "hi";
91-
abbr = "1i";
92-
expected = true;
93-
actual = test.validWordAbbreviation(word, abbr);
94-
assertEquals(expected, actual);
95-
}
96-
97-
@Test
98-
public void test8() {
99-
word = "hi";
100-
abbr = "3";
101-
expected = false;
102-
actual = test.validWordAbbreviation(word, abbr);
103-
assertEquals(expected, actual);
104-
}
105-
106-
@Test
107-
public void test9() {
108-
word = "hi";
109-
abbr = "11";
110-
expected = false;
111-
actual = test.validWordAbbreviation(word, abbr);
112-
assertEquals(expected, actual);
113-
}
114-
115-
@Test
116-
public void test10() {
117-
word = "word";
118-
abbr = "1o1d";
119-
expected = true;
120-
actual = test.validWordAbbreviation(word, abbr);
121-
assertEquals(expected, actual);
122-
}
123-
124-
@Test
125-
public void test11() {
126-
word = "abbreviation";
127-
abbr = "a010n";
128-
expected = false;
129-
actual = test.validWordAbbreviation(word, abbr);
130-
assertEquals(expected, actual);
131-
}
11+
private static _408.Solution1 solution1;
12+
private static Boolean expected;
13+
private static Boolean actual;
14+
private static String word;
15+
private static String abbr;
16+
17+
@BeforeClass
18+
public static void setup() {
19+
solution1 = new _408.Solution1();
20+
}
21+
22+
@Before
23+
public void setupForEachTest() {
24+
word = "";
25+
abbr = "";
26+
}
27+
28+
@Test
29+
public void test1() {
30+
word = "internationalization";
31+
abbr = "i12iz4n";
32+
expected = true;
33+
actual = solution1.validWordAbbreviation(word, abbr);
34+
assertEquals(expected, actual);
35+
}
36+
37+
@Test
38+
public void test2() {
39+
word = "apple";
40+
abbr = "a2e";
41+
expected = false;
42+
actual = solution1.validWordAbbreviation(word, abbr);
43+
assertEquals(expected, actual);
44+
}
45+
46+
@Test
47+
public void test3() {
48+
word = "internationalization";
49+
abbr = "i5a11o1";
50+
expected = true;
51+
actual = solution1.validWordAbbreviation(word, abbr);
52+
assertEquals(expected, actual);
53+
}
54+
55+
@Test
56+
public void test4() {
57+
word = "hi";
58+
abbr = "1";
59+
expected = false;
60+
actual = solution1.validWordAbbreviation(word, abbr);
61+
assertEquals(expected, actual);
62+
}
63+
64+
@Test
65+
public void test5() {
66+
word = "a";
67+
abbr = "1";
68+
expected = true;
69+
actual = solution1.validWordAbbreviation(word, abbr);
70+
assertEquals(expected, actual);
71+
}
72+
73+
@Test
74+
public void test6() {
75+
word = "a";
76+
abbr = "2";
77+
expected = false;
78+
actual = solution1.validWordAbbreviation(word, abbr);
79+
assertEquals(expected, actual);
80+
}
81+
82+
@Test
83+
public void test7() {
84+
word = "hi";
85+
abbr = "1i";
86+
expected = true;
87+
actual = solution1.validWordAbbreviation(word, abbr);
88+
assertEquals(expected, actual);
89+
}
90+
91+
@Test
92+
public void test8() {
93+
word = "hi";
94+
abbr = "3";
95+
expected = false;
96+
actual = solution1.validWordAbbreviation(word, abbr);
97+
assertEquals(expected, actual);
98+
}
99+
100+
@Test
101+
public void test9() {
102+
word = "hi";
103+
abbr = "11";
104+
expected = false;
105+
actual = solution1.validWordAbbreviation(word, abbr);
106+
assertEquals(expected, actual);
107+
}
108+
109+
@Test
110+
public void test10() {
111+
word = "word";
112+
abbr = "1o1d";
113+
expected = true;
114+
actual = solution1.validWordAbbreviation(word, abbr);
115+
assertEquals(expected, actual);
116+
}
117+
118+
@Test
119+
public void test11() {
120+
word = "abbreviation";
121+
abbr = "a010n";
122+
expected = false;
123+
actual = solution1.validWordAbbreviation(word, abbr);
124+
assertEquals(expected, actual);
125+
}
132126
}

0 commit comments

Comments
 (0)