Suppose we have a string s, we repeatedly delete the first consecutive duplicate characters. We have to find the final string.
So, if the input is like s = "xyyyxxz", then the output will be "z", as "yyy" are the first consecutive duplicate characters which will be deleted. So we have "xxxz". Then "xxx" will be deleted to end up with "z".
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
class Solution: def solve(self, s): stack =  i = 0 while i < len(s): if len(stack) and stack[-1] == s[i]: x = stack.pop() while i < len(s) and x == s[i]: i += 1 i -= 1 else: stack.append(s[i]) i += 1 return "".join(stack) ob = Solution() s = "xyyyxxz" print(ob.solve(s))