-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path0161-one-edit-distance.js
More file actions
36 lines (31 loc) · 985 Bytes
/
0161-one-edit-distance.js
File metadata and controls
36 lines (31 loc) · 985 Bytes
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
/**
* 161. One Edit Distance
* https://leetcode.com/problems/one-edit-distance/
* Difficulty: Medium
*
* Given two strings s and t, return true if they are both one edit distance apart, otherwise
* return false.
*
* A string s is said to be one distance apart from a string t if you can:
* - Insert exactly one character into s to get t.
* - Delete exactly one character from s to get t.
* - Replace exactly one character of s with a different character to get t.
*/
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isOneEditDistance = function(s, t) {
if (s === t) return false;
const sLength = s.length;
const tLength = t.length;
if (Math.abs(sLength - tLength) > 1) return false;
if (sLength > tLength) return isOneEditDistance(t, s);
let i = 0;
while (i < sLength && s[i] === t[i]) i++;
if (sLength === tLength) {
return i < sLength && s.slice(i + 1) === t.slice(i + 1);
}
return s.slice(i) === t.slice(i + 1);
};