Remove Adjacent Duplicates
Programming Interview
Medium
5 views
Problem Description
For each testcase you get string s. Repeatedly remove adjacent equal characters using a stack idea. Output final string (or EMPTY).
Input Format
First line t. Next t lines strings.
Output Format
t lines final strings.
Sample Test Case
Input:
3
aabbcc
abba
azxxzy
Official Solution
import sys
lines=sys.stdin.read().splitlines()
if not lines:
sys.exit(0)
try:
t=int(lines[0].strip())
except Exception:
t=0
idx=1
out=[]
for _ in range(t):
s=lines[idx] if idx<len(lines) else ''
idx+=1
st=[]
for ch in s:
if st and st[-1]==ch:
st.pop()
else:
st.append(ch)
out.append(''.join(st) if st else 'EMPTY')
sys.stdout.write('\
'.join(out))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!