Neutralisation of charges (hackerearth.1)

 In a parallel universe, there are not just two charges like positive and negative, but there are 

26 charges represented by  lower 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 s where each si represents a charge, where 0i|s|1.

You need to output size of final string followed by string after which no neutralizing is possible.

SAMPLE INPUT
 
12
aaacccbbcccd
SAMPLE OUTPUT
 
2
ad
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[-1and stack[-2]:
            if stack[-1] == stack[-2]:
                stack.pop()
                stack.pop()
    except:
        pass
print(len(stack))

print(''.join(stack))

Comments

Popular posts from this blog

c++ oops

Takeoff (hackerearth.3, datastructure, array 1-D)

Aptitude tricks