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.
Rather simplistic greedy approach (Python3) in 30 lines total:
fromcollectionsimportCounterdefreverseShuffleMerge(s):# find character frequencies in the desired string Afreq={key:val// 2 for key, val in Counter(s).items()}# repeat gready approach until solution is completesolution=[]n=len(s)whilelen(solution)<n// 2:left_freq=Counter(s)next_char={}min_char='~'# search original string S backwardsforindexinrange(len(s)-1,-1,-1):char=s[index]# it is possible to take this char as the next oneifcharnotinnext_charandfreq[char]>0:next_char[char]=indexmin_char=min(min_char,char)# there's not enough letters available to the leftleft_freq[char]-=1ifleft_freq[char]<freq[char]:break# add minimal char as the next one (greedy)solution+=min_charfreq[min_char]-=1s=s[0:next_char[min_char]]return"".join(solution)
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 →
Rather simplistic greedy approach (Python3) in 30 lines total: