Steve has a string of lowercase characters in vary ascii[‘a’..’z’]. He desires to cut back the string to its shortest size by doing a sequence of operations. In every operation he selects a pair of adjoining lowercase letters that match, and he deletes them. As an illustration, the string aab may very well be shortened to b in a single operation.
Steve’s process is to delete as many characters as attainable utilizing this methodology and print the ensuing string. If the ultimate string is empty, print Empty String
Take a look at Case 1 :
output 1 :
Take a look at Case 2 :
output 2 :
def test(s): for i in vary(len(s)-1): if(s[i] == s[i+1]): return(False) return(True) def take away(s): if(test(s)): return(s) else: for i in vary(len(s)-1): if(s[i] == s[i+1]): if(i+2 < len(s)): return(take away(s[0:i]+s[i+2:])) else: return(take away(s[0:i])) s=checklist(enter()) s2=take away(s) if(len(s2)>0): print(''.be part of(s2)) else: print('Empty String')
However I’m confused the right way to make an iterative method of this drawback.Can anybody assist me?