Skip to content

Instantly share code, notes, and snippets.

@RP-3
Created February 22, 2020 05:02
Show Gist options
  • Save RP-3/5c0086987031f9107d85404dd64bb335 to your computer and use it in GitHub Desktop.
Save RP-3/5c0086987031f9107d85404dd64bb335 to your computer and use it in GitHub Desktop.
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