-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path161.one-edit-distance.js
94 lines (85 loc) · 1.64 KB
/
161.one-edit-distance.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* @lc app=leetcode id=161 lang=javascript
*
* [161] One Edit Distance
*
* https://leetcode.com/problems/one-edit-distance/description/
*
* algorithms
* Medium (32.18%)
* Likes: 610
* Dislikes: 111
* Total Accepted: 107.9K
* Total Submissions: 334.5K
* Testcase Example: '"ab"\n"acb"'
*
* Given two strings s and t, determine if they are both one edit distance
* apart.
*
* Note:
*
* There are 3 possiblities to satisify one edit distance apart:
*
*
* Insert a character into s to get t
* Delete a character from s to get t
* Replace a character of s to get t
*
*
* Example 1:
*
*
* Input: s = "ab", t = "acb"
* Output: true
* Explanation: We can insert 'c' into s to get t.
*
*
* Example 2:
*
*
* Input: s = "cab", t = "ad"
* Output: false
* Explanation: We cannot get t from s by only one step.
*
* Example 3:
*
*
* Input: s = "1203", t = "1213"
* Output: true
* Explanation: We can replace '0' with '1' to get t.
*
*/
// @lc code=start
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
/*
//just cover the 3 scenario
abc
*/
var isOneEditDistance = function (s, t) {
const longer = s.length > t.length ? s : t;
const shorter = s.length > t.length ? t : s;
if (longer.length - shorter.length > 1) {
return false;
}
let count = 0,
i = 0,
j = 0;
//one cover 3 sample case, pretty smart
while (i < longer.length) {
if (longer[i] !== shorter[j]) {
//add one letter that is doesn't match
count++;
if (longer.length > shorter.length) {
i++;
continue;
}
}
i++;
j++;
}
return count === 1;
};