We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I did mine in python, however my solution is messy as hell especially since I ended up reaching the max recursion in the one test case with one of the larger strings.
I ended up having to split strings into substrings, cat them and check if that string was reduced again, rinse and repeat.
But the main idea I had was to use 2 recursive functions, one which did the reducing and one that checks if the string is reduced, the rest is in code.
defreduce_str(s,index):""" reduces the string recursively """last_index=len(s)-1if(len(s)==0):return'Empty String'ifstart>=last_index:ifis_reduced(s,0):returnselse:returnreduced(s,0)elifs[index]==s[index+1]:ifindex+1<last_index:returnreduced(s[:index]+s[index+2:],index)elifend==last_index:returnreduced(s[:index],index)else:returnreduced(s,index+1)defis_reduced(s,index):""" checks if the string is reduced, returns a bool """last_index=len(s)-1ifstart==last_indexorlen(s)==0:returnTrueelifs[index]!=s[index+1]:returnis_reduced(s,index+1)else:returnFalse
Super Reduced String
You are viewing a single comment's thread. Return to all comments →
I did mine in python, however my solution is messy as hell especially since I ended up reaching the max recursion in the one test case with one of the larger strings.
I ended up having to split strings into substrings, cat them and check if that string was reduced again, rinse and repeat.
But the main idea I had was to use 2 recursive functions, one which did the reducing and one that checks if the string is reduced, the rest is in code.