We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Super Reduced String
- Discussions
Super Reduced String
Super Reduced String
Sort by
recency
|
47 Discussions
|
Please Login in order to post a comment
Java 15
C++ Trick is to use a stack, but can just use a string the same way. Moving from left to right, if the current char is the same as on the top of stack (or .back() of string) pop it (pop_back()), and do not push it to stack, effectivly deleting them. Else we push character to the stack (push_back()). If we use a string as a stack, we can just return this resulting string;
def superReducedString(s): stack = [] for n in s: if not stack or stack[-1] != n: stack.append(n) else: stack.pop() if not stack: return 'Empty String' return "".join(stack)
!/bin/python3
def superReducedString(s): # Write your code here stack = [] for char in s: if stack and stack[-1] == char: stack.pop() else: stack.append(char) reduced_string = ''.join(stack) return reduced_string if reduced_string else 'Empty String'
if name == 'main': fptr = open(os.environ['OUTPUT_PATH'], 'w')
My answer in Python