-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentence-similarity-iii.js
54 lines (49 loc) · 1.22 KB
/
sentence-similarity-iii.js
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
/**
* 1813. 句子相似性 III
* @param {string} sentence1
* @param {string} sentence2
* @return {boolean}
*/
var areSentencesSimilar = function (sentence1, sentence2) {
sentence1 = sentence1.split(" ");
sentence2 = sentence2.split(" ");
while (sentence1.length && sentence2.length) {
if (sentence1[0] === sentence2[0]) {
sentence1.shift();
sentence2.shift();
} else if (sentence1.at(-1) === sentence2.at(-1)) {
sentence1.pop();
sentence2.pop();
} else {
return false;
}
}
return true;
// return areSentencesSimilar2(sentence1, sentence2);
};
// 双端指针解法
var areSentencesSimilar2 = function (sentence1, sentence2) {
sentence1 = sentence1.split(' ')
sentence2 = sentence2.split(' ')
let left1 = 0;
let right1 = sentence1.length - 1;
let left2 = 0;
let right2 = sentence2.length - 1;
while (left1 <= right1 && left2 <= right2) {
if (
sentence1[left1] !== sentence2[left2] &&
sentence1[right1] !== sentence2[right2]
) {
return false;
}
if (sentence1[left1] === sentence2[left2]) {
left1++;
left2++;
}
if (sentence1[right1] === sentence2[right2]) {
right1--;
right2--;
}
}
return true;
};