Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3e05583

Browse files
refactor 408
1 parent96025bc commit3e05583

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
packagecom.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
publicclass_408 {
2427

25-
publicbooleanvalidWordAbbreviation(Stringword,Stringabbr) {
26-
if (abbr.length() >word.length()) {
27-
returnfalse;
28-
}else {
29-
char[]abbrChars =abbr.toCharArray();
30-
char[]wordChars =word.toCharArray();
31-
if (abbr.length() ==word.length()) {
32-
booleanprevDigit =false;
33-
for (inti =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+
publicstaticclassSolution1 {
29+
publicbooleanvalidWordAbbreviation(Stringword,Stringabbr) {
30+
if (abbr.length() >word.length()) {
31+
returnfalse;
32+
}else {
33+
char[]abbrChars =abbr.toCharArray();
34+
char[]wordChars =word.toCharArray();
35+
if (abbr.length() ==word.length()) {
36+
booleanprevDigit =false;
37+
for (inti =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+
returnfalse;
42+
}
43+
}elseif (Character.isDigit(abbrChars[i]) &&prevDigit) {
3744
returnfalse;
45+
}elseif (abbrChars[i] !=wordChars[j]) {
46+
returnfalse;
47+
}elseif (prevDigit) {
48+
prevDigit =false;
3849
}
39-
}elseif (Character.isDigit(abbrChars[i]) &&prevDigit) {
40-
returnfalse;
41-
}elseif (abbrChars[i] !=wordChars[j]) {
42-
returnfalse;
43-
}elseif (prevDigit) {
44-
prevDigit =false;
4550
}
46-
}
47-
returntrue;
48-
}else {
49-
StringBuilderstringBuilder =newStringBuilder();
50-
booleanfirstDigit =true;
51-
for (inti =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+
returntrue;
52+
}else {
53+
StringBuilderstringBuilder =newStringBuilder();
54+
booleanfirstDigit =true;
55+
for (inti =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+
returnfalse;
59+
}
60+
stringBuilder.append(abbrChars[i]);
61+
i++;
62+
firstDigit =false;
63+
}
64+
firstDigit =true;
65+
if (!stringBuilder.toString().isEmpty()) {
66+
intnumber =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
returnfalse;
5573
}
56-
stringBuilder.append(abbrChars[i]);
57-
i++;
58-
firstDigit =false;
59-
}
60-
firstDigit =true;
61-
if (!stringBuilder.toString().isEmpty()) {
62-
intnumber =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-
returnfalse;
68-
}
69-
if (i <abbrChars.length &&j <wordChars.length &&abbrChars[i] !=wordChars[j]) {
70-
returnfalse;
71-
}
72-
if (j >wordChars.length &&i <=abbrChars.length) {
73-
returnfalse;
74+
if (i <abbrChars.length
75+
&&j <wordChars.length
76+
&&abbrChars[i] !=wordChars[j]) {
77+
returnfalse;
78+
}
79+
if (j >wordChars.length &&i <=abbrChars.length) {
80+
returnfalse;
81+
}
82+
j++;
7483
}
75-
j++;
84+
returntrue;
7685
}
77-
returntrue;
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
importstaticjunit.framework.Assert.assertEquals;
99

10-
/**
11-
* Created by fishercoder on 1/21/17.
12-
*/
1310
publicclass_408Test {
14-
privatestatic_408test;
15-
privatestaticBooleanexpected;
16-
privatestaticBooleanactual;
17-
privatestaticStringword;
18-
privatestaticStringabbr;
19-
20-
@BeforeClass
21-
publicstaticvoidsetup() {
22-
test =new_408();
23-
}
24-
25-
@Before
26-
publicvoidsetupForEachTest() {
27-
word ="";
28-
abbr ="";
29-
}
30-
31-
@Test
32-
publicvoidtest1() {
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-
publicvoidtest2() {
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-
publicvoidtest3() {
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-
publicvoidtest4() {
63-
word ="hi";
64-
abbr ="1";
65-
expected =false;
66-
actual =test.validWordAbbreviation(word,abbr);
67-
assertEquals(expected,actual);
68-
}
69-
70-
@Test
71-
publicvoidtest5() {
72-
word ="a";
73-
abbr ="1";
74-
expected =true;
75-
actual =test.validWordAbbreviation(word,abbr);
76-
assertEquals(expected,actual);
77-
}
78-
79-
@Test
80-
publicvoidtest6() {
81-
word ="a";
82-
abbr ="2";
83-
expected =false;
84-
actual =test.validWordAbbreviation(word,abbr);
85-
assertEquals(expected,actual);
86-
}
87-
88-
@Test
89-
publicvoidtest7() {
90-
word ="hi";
91-
abbr ="1i";
92-
expected =true;
93-
actual =test.validWordAbbreviation(word,abbr);
94-
assertEquals(expected,actual);
95-
}
96-
97-
@Test
98-
publicvoidtest8() {
99-
word ="hi";
100-
abbr ="3";
101-
expected =false;
102-
actual =test.validWordAbbreviation(word,abbr);
103-
assertEquals(expected,actual);
104-
}
105-
106-
@Test
107-
publicvoidtest9() {
108-
word ="hi";
109-
abbr ="11";
110-
expected =false;
111-
actual =test.validWordAbbreviation(word,abbr);
112-
assertEquals(expected,actual);
113-
}
114-
115-
@Test
116-
publicvoidtest10() {
117-
word ="word";
118-
abbr ="1o1d";
119-
expected =true;
120-
actual =test.validWordAbbreviation(word,abbr);
121-
assertEquals(expected,actual);
122-
}
123-
124-
@Test
125-
publicvoidtest11() {
126-
word ="abbreviation";
127-
abbr ="a010n";
128-
expected =false;
129-
actual =test.validWordAbbreviation(word,abbr);
130-
assertEquals(expected,actual);
131-
}
11+
privatestatic_408.Solution1solution1;
12+
privatestaticBooleanexpected;
13+
privatestaticBooleanactual;
14+
privatestaticStringword;
15+
privatestaticStringabbr;
16+
17+
@BeforeClass
18+
publicstaticvoidsetup() {
19+
solution1 =new_408.Solution1();
20+
}
21+
22+
@Before
23+
publicvoidsetupForEachTest() {
24+
word ="";
25+
abbr ="";
26+
}
27+
28+
@Test
29+
publicvoidtest1() {
30+
word ="internationalization";
31+
abbr ="i12iz4n";
32+
expected =true;
33+
actual =solution1.validWordAbbreviation(word,abbr);
34+
assertEquals(expected,actual);
35+
}
36+
37+
@Test
38+
publicvoidtest2() {
39+
word ="apple";
40+
abbr ="a2e";
41+
expected =false;
42+
actual =solution1.validWordAbbreviation(word,abbr);
43+
assertEquals(expected,actual);
44+
}
45+
46+
@Test
47+
publicvoidtest3() {
48+
word ="internationalization";
49+
abbr ="i5a11o1";
50+
expected =true;
51+
actual =solution1.validWordAbbreviation(word,abbr);
52+
assertEquals(expected,actual);
53+
}
54+
55+
@Test
56+
publicvoidtest4() {
57+
word ="hi";
58+
abbr ="1";
59+
expected =false;
60+
actual =solution1.validWordAbbreviation(word,abbr);
61+
assertEquals(expected,actual);
62+
}
63+
64+
@Test
65+
publicvoidtest5() {
66+
word ="a";
67+
abbr ="1";
68+
expected =true;
69+
actual =solution1.validWordAbbreviation(word,abbr);
70+
assertEquals(expected,actual);
71+
}
72+
73+
@Test
74+
publicvoidtest6() {
75+
word ="a";
76+
abbr ="2";
77+
expected =false;
78+
actual =solution1.validWordAbbreviation(word,abbr);
79+
assertEquals(expected,actual);
80+
}
81+
82+
@Test
83+
publicvoidtest7() {
84+
word ="hi";
85+
abbr ="1i";
86+
expected =true;
87+
actual =solution1.validWordAbbreviation(word,abbr);
88+
assertEquals(expected,actual);
89+
}
90+
91+
@Test
92+
publicvoidtest8() {
93+
word ="hi";
94+
abbr ="3";
95+
expected =false;
96+
actual =solution1.validWordAbbreviation(word,abbr);
97+
assertEquals(expected,actual);
98+
}
99+
100+
@Test
101+
publicvoidtest9() {
102+
word ="hi";
103+
abbr ="11";
104+
expected =false;
105+
actual =solution1.validWordAbbreviation(word,abbr);
106+
assertEquals(expected,actual);
107+
}
108+
109+
@Test
110+
publicvoidtest10() {
111+
word ="word";
112+
abbr ="1o1d";
113+
expected =true;
114+
actual =solution1.validWordAbbreviation(word,abbr);
115+
assertEquals(expected,actual);
116+
}
117+
118+
@Test
119+
publicvoidtest11() {
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)

[8]ページ先頭

©2009-2025 Movatter.jp