相隔为1 的编辑距离。
- 时间复杂度O(n),空间复杂度O(1)
- Runtime: 84 ms, faster than 37.27%
- Memory Usage: 38.6 MB, less than 99.38%
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isOneEditDistance = function(s, t) {
let sLen = s.length;
let tLen = t.length;
if (sLen > tLen) {
return isOneEditDistance(t, s);
}
if (tLen - sLen > 1) {
return false;
}
for (let i = 0; i < sLen; i++) {
if (s[i] !== t[i]) {
if (sLen === tLen) {
return s.substring(i + 1) === t.substring(i + 1);
} else {
return s.substring(i) === t.substring(i + 1);
}
}
}
return sLen + 1 === tLen;
};
网友评论