You are viewing a single comment's thread. Return to all comments →
A Java solution
Iterate reversely. If seeing any character behinds c[i] is bigger than c[i], swap that character with c[i] and sort the remaining characters behind.
c[i]
public static String biggerIsGreater(String w) { int n = w.length(); char[] c = w.toCharArray(); StringBuilder sb = new StringBuilder(); for (int i = n-2; i >= 0; i--) { for (int j = n-1; j > i; j--) { if (c[j] > c[i]) { sb.append(w.substring(0, i)); sb.append(c[j]); String temp = w.substring(i, j) + w.substring(j+1); char[] c2 = temp.toCharArray(); Arrays.sort(c2); sb.append(c2); return sb.toString(); } } } return "no answer"; }
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 →
A Java solution
Iterate reversely. If seeing any character behinds
c[i]is bigger thanc[i], swap that character withc[i]and sort the remaining characters behind.