Created
February 22, 2020 05:02
-
-
Save RP-3/5c0086987031f9107d85404dd64bb335 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var isOneEditDistance = function(s, t) { | |
if(Math.abs(s.length - t.length) > 1) return false; | |
if(s === t) return false; | |
let longer; let shorter; | |
if(s.length > t.length){ | |
[longer, shorter] = [s, t]; | |
}else{ | |
[longer, shorter] = [t, s]; | |
} | |
for(let i=0; i<longer.length; i++){ | |
if(longer[i] !== shorter[i]){ | |
return ( | |
longer.slice(i+1) === shorter.slice(i) || // insert into shorter | |
// delete from longer. Same as add to shorter so don't bother | |
longer.slice(i+1) === shorter.slice(i+1) // edit to match | |
) | |
} | |
} | |
return true; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment