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.
defreverseShuffleMerge(s):"""Straightforwardimplementationusingonly2dictionaries:-c_reversed-numberofsymbolsleftinareversedpart-c_shuffled-numberofsymbolsleftinashuffledpartandfixedthe'result'arraytoexcludethe'pop'operation."""c_reversed={k:v// 2 for k, v in Counter(s).items()}c_shuffled=c_reversed.copy()result,rlen=[' ']*(len(s)// 2), 0forcharinreversed(s):ifc_reversed[char]:whilerlenandresult[rlen-1]>charandc_shuffled[result[rlen-1]]:rlen-=1c_reversed[result[rlen]]+=1c_shuffled[result[rlen]]-=1c_reversed[char]-=1result[rlen]=charrlen+=1else:c_shuffled[char]-=1return''.join(result)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Reverse Shuffle Merge
You are viewing a single comment's thread. Return to all comments →
Python 3: