Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 7401afe

Browse files
refactor 408
1 parent 17c9773 commit 7401afe

File tree

2 files changed

+118
-141
lines changed

2 files changed

+118
-141
lines changed

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

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,5 @@
11
package com.fishercoder.solutions;
22

3-
/**
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:
9-
10-
["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]
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".
13-
14-
Note:
15-
Assume s contains only lowercase letters and abbr contains only lowercase letters and digits.
16-
17-
Example 1:
18-
Given s = "internationalization", abbr = "i12iz4n":
19-
20-
Return true.
21-
Example 2:
22-
Given s = "apple", abbr = "a2e":
23-
24-
Return false.
25-
*/
263
public class _408 {
274

285
public static class Solution1 {
@@ -68,12 +45,12 @@ public boolean validWordAbbreviation(String word, String abbr) {
6845
stringBuilder.setLength(0);
6946
}
7047
if ((i >= abbrChars.length && j < wordChars.length) || (i < abbrChars.length
71-
&& j >= wordChars.length)) {
48+
&& j >= wordChars.length)) {
7249
return false;
7350
}
7451
if (i < abbrChars.length
75-
&& j < wordChars.length
76-
&& abbrChars[i] != wordChars[j]) {
52+
&& j < wordChars.length
53+
&& abbrChars[i] != wordChars[j]) {
7754
return false;
7855
}
7956
if (j > wordChars.length && i <= abbrChars.length) {

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

Lines changed: 115 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -8,119 +8,119 @@
88
import static junit.framework.Assert.assertEquals;
99

1010
public class _408Test {
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-
}
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+
}
126126
}

0 commit comments

Comments
 (0)