-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleet13.java
More file actions
30 lines (23 loc) · 792 Bytes
/
Copy pathleet13.java
File metadata and controls
30 lines (23 loc) · 792 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
26
27
28
29
30
class Solution {
private String[] splitWords(String sentence) {
return sentence.split(" ");
}
public boolean areSentencesSimilar(String sentence1, String sentence2) {
String[] words1 = splitWords(sentence1);
String[] words2 = splitWords(sentence2);
if (words1.length < words2.length) {
String[] temp = words1;
words1 = words2;
words2 = temp;
}
int start = 0, end = 0;
int n1 = words1.length, n2 = words2.length;
while (start < n2 && words1[start].equals(words2[start])) {
start++;
}
while (end < n2 && words1[n1 - end - 1].equals(words2[n2 - end - 1])) {
end++;
}
return start + end >= n2;
}
}