Created
April 5, 2024 17:12
-
-
Save tatsuyax25/29b4af25cfe9b448997752d37c9ae536 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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