Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Mohammed Dh Abbas
Another way to go about it without modifying the top value in the stack

Mohammed Dh Abbas

Jul 31, 2024

class Solution: def removeDuplicates(self, s: str, k: int) -> str: stack = [] for index, char in enumerate(s): # if we have reached k count for the same character if len(stack) > 0 and char == stack[-1][0] and stack[-1][1] == k - 1: # keep popping count = 0 while count < k - 1: count += 1 stack.pop() else: # push the character # if pervious character is the same as the character from the loop if len(stack) > 0 and char == stack[-1][0]: stack.append((char, stack[-1][1] + 1)) else: stack.append((char, 1)) return "".join([x for x,y in stack]);

0

0

Comments
Comments