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
25 lines (23 loc) · 841 Bytes
/
Leetcode_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
public class Leetcode_1813 {
public boolean areSentencesSimilar(String sentence1, String sentence2) {
String[] s1Words = sentence1.split(" ");
String[] s2Words = sentence2.split(" ");
int n1 = s1Words.length;
int n2 = s2Words.length;
int left = 0, r1 = n1 - 1, r2 = n2 - 1;
while (s1Words[left].equals(s2Words[left])) {
left++;
if ((left == n1) || (left == n2)) return true;
}
while (s1Words[r1].equals(s2Words[r2])) {
if ((r1 == left) || (r2 == left)) return true;
r1--;
r2--;
}
return false;
}
public static void main(String[] args) {
Leetcode_1813 obj = new Leetcode_1813();
System.out.println(obj.areSentencesSimilar("My name is Haley", "My Haley"));
}
}