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.
for _ in range(int(input())):
w = input().strip()
n = len(w)+1
for k in range(-2,-n,-1):
if w[k]<w[k+1]:
print(w[:k],end='')
v = w[:k:-1]
for j in range(-k-1):
if w[k]<v[j]:
print(v[j]+v[:j]+w[k]+v[j+1:])
break
else:
print(v+w[k])
break
else:
print('no answer')
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 3 solution: