0% completed
Problem 7: Remove All Adjacent Duplicates In String (medium)
.....
.....
.....
Anonymous
· 2 years ago
Maybe this isn't the "right" approach but since this is a stack problem, iterating over the string in sequential order would end up with a reverse answer, so instead of reversing the stack (which I'm pretty sure goes against the constraints of a stack) I figured you could just iterate over the string from the opposite end. I know JS doesn't include a "peek" method so I decided to just use the stack length to access the ending element (for some reason my browser/code editor won't accept stack.at(-1) but I think this is a bit more reasonable than the given answer in the solution section:
class Solution { removeDuplicates(s) { // ToDo: Write Your Code Here. const stack = [] for (let i = s.length - 1; i > -1; i--) { if (stack.length && stack.at
Anonymous
· 2 years ago
The solution code doesn't match the algorithm and diagram. The solution code is perhaps a better implementation, but you ought to change something so these match.
The algorithm and diagram say that at the end you pop the elements from the stack (which gives you the string in the wrong order), and then you reverse the string.
The code doesn't pop elements from the stack, instead it accesses elements in a for loop so they're already in the correct order.
Reading Progress
0%