Hey I was trying to write a program which will remove the consecutive duplicate characters from a string.
for example:
string->aabbccde
first iteration: bbccde
second iteration: ccde
third Iteration: de
and de is the answer.
following is the program I wrote.
a = "aabbcs"
def remove_dups(st,ind):
print st, ind
st = st.replace(st[ind], "")
print st, "in dups"
find_dups(st)
def find_dups(text):
s=text
print s, "in find"
ln = len(s)
print ln
fg = 0
ind = 0
if ln==1:
print s, 'len'
return s
for i in range(0,ln-1):
if(s[i]==s[i+1]):
ind = i
remove_dups(s,ind)
print s, 'check'
return s
ans = find_dups(a)
print 'answer', ans
and following is the output I am getting
aabbcs in find
6
aabbcs 0
bbcs in dups
bbcs in find
4
bbcs 0
cs in dups
cs in find
2
cs check
bbcs check
aabbcs 2
aacs in dups
aacs in find
4
aacs 0
cs in dups
cs in find
2
cs check
aacs check
aabbcs check
answer aabbcs
here above we have got cs but still answer is coming original string, I can understand it is because of recursion, but unable to understand how to resolve the issue. A little help would be appreciated. Thanks!