Last active
January 29, 2020 19:36
-
-
Save markusait/0a7b344e05ef64daa6396c15fd3e6ef5 to your computer and use it in GitHub Desktop.
Interview question from https://www.youtube.com/watch?v=wyu6VRmtCmE&t=916s
This file contains 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
// https://www.youtube.com/watch?v=wyu6VRmtCmE&t=916s | |
const InsensitiveDistanceCompare = (str1, str2) => { | |
let index1 = 0 | |
let index2 = 0 | |
let different = 0 | |
while(index1 < str1.length && index2 < str2.length) { | |
char1 = str1[index1] | |
char2 = str2[index2] | |
if(char1.toLowerCase() === char2.toLowerCase()) { | |
index1++ | |
index2++ | |
} else { | |
different++ | |
if(different > 1) { | |
return false | |
} else if(s1.length === s2.length) { | |
index1++ | |
index2++ | |
} else if(s1.length < s2.length) { | |
index2++ | |
} else { | |
index1++ | |
} | |
} | |
} | |
return true | |
} | |
let s1, s2 | |
s1="abdeF" | |
s2="abCdeF" | |
console.assert(InsensitiveDistanceCompare(s1,s2) === true, "Should be true") | |
s1 = "abcde" | |
s2 = "abcef" | |
console.assert(InsensitiveDistanceCompare(s1,s2) === false, "Should be false") | |
s1 = "abcddgh" | |
s2 = "abcdfgi" | |
console.assert(InsensitiveDistanceCompare(s1,s2) === false, "Should be false") | |
s1 = "abcddgh" | |
s2 = "abcdfgh" | |
console.assert(InsensitiveDistanceCompare(s1,s2) === true, "Should be true") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment