forked from ghostmkg/programming-language
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode_1813.java
More file actions
Latest commit
25 lines (23 loc) · 841 Bytes
/
Copy pathLeetcode_1813.java
File metadata and controls
25 lines (23 loc) · 841 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
publicclassLeetcode_1813 {
publicbooleanareSentencesSimilar(Stringsentence1, Stringsentence2) {
String[] s1Words = sentence1.split(" ");
String[] s2Words = sentence2.split(" ");
intn1 = s1Words.length;
intn2 = s2Words.length;
intleft = 0, r1 = n1 - 1, r2 = n2 - 1;
while (s1Words[left].equals(s2Words[left])) {
left++;
if ((left == n1) || (left == n2)) returntrue;
}
while (s1Words[r1].equals(s2Words[r2])) {
if ((r1 == left) || (r2 == left)) returntrue;
r1--;
r2--;
}
returnfalse;
}
publicstaticvoidmain(String[] args) {
Leetcode_1813obj = newLeetcode_1813();
System.out.println(obj.areSentencesSimilar("My name is Haley", "My Haley"));
}
}