Skip to content

Instantly share code, notes, and snippets.

@Trend20
Forked from Angelfire/stringReduction.js
Created May 1, 2022 09:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Trend20/c52dbc5f4f9122b196b8e51211028af2 to your computer and use it in GitHub Desktop.
Save Trend20/c52dbc5f4f9122b196b8e51211028af2 to your computer and use it in GitHub Desktop.
String Reduction
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