Skip to content

Commit 6e25d84

Browse files
add 1813
1 parent e97d066 commit 6e25d84

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1813|[Sentence Similarity III](https://leetcode.com/problems/sentence-similarity-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1813.java) ||Medium|String|
1112
|1812|[Determine Color of a Chessboard Square](https://leetcode.com/problems/determine-color-of-a-chessboard-square/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1812.java) ||Easy|String|
1213
|1807|[Evaluate the Bracket Pairs of a String](https://leetcode.com/problems/evaluate-the-bracket-pairs-of-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1807.java) ||Medium|HashTable, String|
1314
|1806|[Minimum Number of Operations to Reinitialize a Permutation](https://leetcode.com/problems/minimum-number-of-operations-to-reinitialize-a-permutation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1806.java) ||Medium|Array, Greedy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1813 {
4+
public static class Solution1 {
5+
public boolean areSentencesSimilar(String sentence1, String sentence2) {
6+
String shorter = sentence1.length() < sentence2.length() ? sentence1 : sentence2;
7+
String longer = shorter.equals(sentence1) ? sentence2 : sentence1;
8+
String[] shortWords = shorter.split(" ");
9+
String[] longWords = longer.split(" ");
10+
int breaks = 0;
11+
int j = 0;
12+
int i = 0;
13+
for (; i < shortWords.length && j < longWords.length; ) {
14+
if (shortWords[i].equals(longWords[j])) {
15+
j++;
16+
i++;
17+
} else {
18+
breaks++;
19+
if (breaks > 1) {
20+
return false;
21+
}
22+
while (j < longWords.length && !longWords[j].equals(shortWords[i])) {
23+
j++;
24+
}
25+
}
26+
}
27+
return (breaks == 1 && i == shortWords.length && j == longWords.length) || (i == shortWords.length && breaks == 0);
28+
}
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1813;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1813Test {
10+
private static _1813.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1813.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(false, solution1.areSentencesSimilar("of", "A lot of words"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(true, solution1.areSentencesSimilar("Eating right now", "Eating"));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(true, solution1.areSentencesSimilar("c h p Ny", "c BDQ r h p Ny"));
30+
}
31+
}

0 commit comments

Comments
 (0)