Super Reduced String

  • + 0 comments

    here is a python solution with O(n) time and O(n) space in worse case; however, if the stack ends up being empty the space will be O(1)

    def superReducedString(s):
        stack = []
        
        for c in s:
            if not stack or stack[-1] != c:
                stack.append(c)
            else:
                stack.pop()
        
        if not stack:
            return "Empty String"
            
        return "".join(stack)