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

Commit25ba7da

Browse files
update 1813
1 parent06b9f6b commit25ba7da

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

‎src/main/java/com/fishercoder/solutions/_1813.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,35 @@ public boolean areSentencesSimilar(String sentence1, String sentence2) {
1717
}else {
1818
breaks++;
1919
if (breaks >1) {
20-
returnfalse;
20+
break;
2121
}
2222
while (j <longWords.length && !longWords[j].equals(shortWords[i])) {
2323
j++;
2424
}
2525
}
2626
}
27-
return (breaks ==1 &&i ==shortWords.length &&j ==longWords.length) || (i ==shortWords.length &&breaks ==0);
27+
if ((breaks ==1 &&i ==shortWords.length &&j ==longWords.length) || (i ==shortWords.length &&breaks ==0)) {
28+
returntrue;
29+
}
30+
//we'll check from the left side and move towards the right side
31+
i =shortWords.length -1;
32+
j =longWords.length -1;
33+
breaks =0;
34+
for (;i >=0 &&j >=0; ) {
35+
if (shortWords[i].equals(longWords[j])) {
36+
i--;
37+
j--;
38+
}else {
39+
breaks++;
40+
if (breaks >1) {
41+
returnfalse;
42+
}
43+
while (j >=0 && !longWords[j].equals(shortWords[i])) {
44+
j--;
45+
}
46+
}
47+
}
48+
return (breaks ==1 &&i == -1 &&j == -1) || (breaks ==0 &&i == -1);
2849
}
2950
}
3051
}

‎src/test/java/com/fishercoder/_1813Test.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,9 @@ public void test2() {
2828
publicvoidtest3() {
2929
assertEquals(true,solution1.areSentencesSimilar("c h p Ny","c BDQ r h p Ny"));
3030
}
31+
32+
@Test
33+
publicvoidtest4() {
34+
assertEquals(true,solution1.areSentencesSimilar("A","a A b A"));
35+
}
3136
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp