-
Notifications
You must be signed in to change notification settings - Fork 0
/
1813. Sentence similarity III
57 lines (47 loc) · 1.28 KB
/
1813. Sentence similarity III
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//1813. Sentence similarity III
class Solution {
private List<String> convert(String sentence) {
sentence += " ";
List<String> ans = new ArrayList<>();
StringBuilder word = new StringBuilder();
for(int i = 0; i < sentence.length(); i++) {
if(sentence.charAt(i) == ' ') {
ans.add(word.toString());
word.setLength(0);
}
else {
word.append(sentence.charAt(i));
}
}
return ans;
}
public boolean areSentencesSimilar(String x, String y) {
if(x.length() < y.length()) {
String temp = x;
x = y;
y = temp;
}
List<String> vx = convert(x);
List<String> vy = convert(y);
int l = 0;
for(int i = 0; i < vy.size(); i++) {
if (vx.get(i).equals(vy.get(i))) {
l++;
}
else {
break;
}
}
int ind = vx.size() - 1, r = vy.size();
for(int i = vy.size() - 1; i >= 0; i--) {
if(vy.get(i).equals(vx.get(ind))) {
ind--;
r = i;
}
else {
break;
}
}
return r <= l;
}
}