-
Notifications
You must be signed in to change notification settings - Fork 41
/
solution.ts
40 lines (38 loc) · 893 Bytes
/
solution.ts
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
function isOneEditDistance (s: string, t: string): boolean {
if (Math.abs(s.length - t.length) > 1) {
return false;
}
if (s.length === t.length) {
let diffFlag = false;
for (let i = 0; i < s.length; i++) {
if (s[i] === t[i]) {
continue;
}
if (diffFlag) {
return false;
}
diffFlag = true;
}
return diffFlag;
}
if (s.length < t.length) {
const tmp = s;
s = t;
t = 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) {
index1++;
continue;
}
return false;
}
return true;
}