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.
defbiggerIsGreater(w):# i is the index of the element on the right hand-sideindex=Noneforiinrange(len(w)-1,-1,-1):w_list=sorted(list(w[i:]))w_list.reverse()sorted_w=""forcharinw_list:sorted_w+=charifsorted_w!=w[i:]:index=ibreakifindexisNone:return"no answer"smallest_bigger=Noneforiinw[index:]:ifi>w[index]and(smallest_biggerisNoneori<smallest_bigger):smallest_bigger=iw_list=list(w[index+1:])w_list.remove(smallest_bigger)w_list.append(w[index])w_list.sort()sorted_w=""forcharinw_list:sorted_w+=charw=w[:index]+smallest_bigger+sorted_wreturnw
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Bigger is Greater
You are viewing a single comment's thread. Return to all comments →
Python3