Neutralisation of charges (hackerearth.1)
In a parallel universe, there are not just two charges like positive and negative, but there are
charges represented by english alphabets.
Charges have a property of killing each other or in other words neutralizing each other if they are of same charge and next to each other.
You are given a string where each represents a charge, where .
You need to output of final string followed by string after which no neutralizing is possible.
Explanation
aaacccbbcccd -> accd -> ad
n = int(input())s = input()l = list(s)stack = []stack.append(l[0])for i in range(1, len(l)):stack.append(l[i])try:if stack[-1] and stack[-2]:if stack[-1] == stack[-2]:stack.pop()stack.pop()except:passprint(len(stack))print(''.join(stack))
Comments
Post a Comment