I am trying to remove consecutively same characters from a string. For example:
abb --> ab
aaab --> ab
ababa --> ababa (since no two consecutive characters are same)
My code:
T=int(input())
l=[0]
S1=""
for i in range(T):
S=input()
for j in range(len(S)-1):
if S[j]!=S[j+1]:
if S[j] != l[len(l)-1]:
l=[]
l.append(S[j])
l.append(S[j+1])
print(l)
for k in l:
S1+=k
print(S1)
S1=""
l=[0]
The code doesn't work for the third case (ababa
). How do I fix this?