-
Notifications
You must be signed in to change notification settings - Fork 41
/
solution.js
46 lines (43 loc) · 944 Bytes
/
solution.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
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isOneEditDistance = function (s, t) {
const diff = Math.abs(s.length - t.length);
if (diff > 1) {
return false;
}
if (s.length === t.length) {
let changed = false;
for (let i = 0; i < s.length; i++) {
if (s[i] === t[i]) {
continue;
}
if (changed) {
return false;
}
changed = true;
}
return changed;
}
if (t.length > s.length) {
const tmp = t;
t = s;
s = tmp;
}
let index1 = 0;
let index2 = 0;
while (index1 < s.length && index2 < t.length) {
if (s[index1] === t[index2]) {
index1++;
index2++;
continue;
}
if (index1 !== index2) {
return false;
}
index1++;
}
return true;
};