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(s):# Convert the string to a list for easy manipulations=list(s)# Find the rightmost character that is smaller than the character to its righti=len(s)-2whilei>=0ands[i]>=s[i+1]:i-=1# If no such character is found, the string is the last permutationifi==-1:return"no answer"# Find the smallest character to the right of i that is larger than s[i]j=len(s)-1whiles[j]<=s[i]:j-=1# Swap the characters at positions i and js[i],s[j]=s[j],s[i]# Reverse the substring to the right of is[i+1:]=reversed(s[i+1:])# Convert the list back to a stringresult="".join(s)returnresult
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 →
My Python Solution: