Skip to content

Instantly share code, notes, and snippets.

@tatsuyax25
Created April 5, 2024 17:12
Show Gist options
  • Save tatsuyax25/29b4af25cfe9b448997752d37c9ae536 to your computer and use it in GitHub Desktop.
Save tatsuyax25/29b4af25cfe9b448997752d37c9ae536 to your computer and use it in GitHub Desktop.
var makeGood = function(s) {
// Create a stack to keep of the characters
let stack = [];
// Iterate over each character in the string
for (let i = 0; i < s.length; i++) {
// If the stack is not empty and the current character is the opposite case of the last character in the stack
if (stack.length > 0 && Math.abs(s.charCodeAt(i) - stack[stack.length - 1].charCodeAt(0)) === 32) {
// Remove the last character from the stack
stack.pop();
} else {
// Otherwise, add the current character to the stack
stack.push(s[i]);
}
}
// Join the remaining characters in the stack into a string and return it
return stack.join('');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment