Last active
May 7, 2022 23:25
-
-
Save Angelfire/5c7ea31c7141ccb25054db661cda0d1f to your computer and use it in GitHub Desktop.
String Reduction
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
function getMinDeletions(s) { | |
const ordered_string = s.split('').sort().join(''); | |
let delCount = 0; | |
for (let i = 0; i < ordered_string.length; i++) { | |
if (ordered_string[i] === ordered_string[i + 1]) { | |
delCount++; | |
} | |
} | |
return delCount; | |
} | |
function getMinDeletionsSet(str){ | |
let strSet = new Set(str); | |
return str.length - strSet.size; | |
} | |
function getMinDeletionsArrayFrom(str) { | |
const sortedStr= Array.from(str).sort(); | |
let delCount = 0; | |
for (let i = 0; i < sortedStr.length; i++) { | |
if (sortedStr[i] === sortedStr[i + 1]) { | |
delCount++; | |
} | |
} | |
return delCount; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment