How to remove a pair of same adjacent characters from string in Python

1 Answer

0 votes
def remove_adjacent_pair(s): 
    print(s)
    i = 1
    while i < len(s): 
        if (s[i] is s[i - 1]): 
            s = s.replace(s[i - 1], '', 2) 
            i = 1
            print(len(s))
            print(s)
        i = i + 1
  
    return s; 
    
     
s = "aabcccdeeffffgac"; 
     
s = remove_adjacent_pair(s)
  
print(s); 
  
  
     
  
'''
run:
  
aabcccdeeffffgac
14
bcccdeeffffgac
12
bcdeeffffgac
10
bcdffffgac
8
bcdffgac
6
bcdgac
bcdgac
 
'''

 



answered Jun 19, 2020 by avibootz
edited Jun 19, 2020 by avibootz
...