class NextPermutation {
public static void nextPermutation(int[] num) {
int n = num.length;
if (n < 2)
return;
int index = n - 1;
while (index > 0) {
if (num[index - 1] < num[index])
break;
index--;
}
if (index == 0) {
reverseSort(num, 0, n - 1);
return;
} else {
int val = num[index - 1];
int j = n - 1;
while (j >= index) {
if (num[j] > val)
break;
j--;
}
swap(num, j, index - 1);
reverseSort(num, index, n - 1);
return;
}
}
public static void swap(int[] num, int i, int j) {
int temp = 0;
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
public static void reverseSort(int[] num, int start, int end) {
if (start > end)
return;
for (int i = start; i <= (end + start) / 2; i++)
swap(num, i, start + end - i);
}
public static void main
(String[] args
) { int[] x = new int[s.length()];
for (int i = 0; i < s.length(); i++) {
x[i] = s.charAt(i);
}
nextPermutation(x);
for (int i : x) {
}
}
}
Y2xhc3MgTmV4dFBlcm11dGF0aW9uIHsKCiAgcHVibGljIHN0YXRpYyB2b2lkIG5leHRQZXJtdXRhdGlvbihpbnRbXSBudW0pIHsKICAgIGludCBuID0gbnVtLmxlbmd0aDsKICAgIGlmIChuIDwgMikKICAgICAgcmV0dXJuOwogICAgaW50IGluZGV4ID0gbiAtIDE7CiAgICB3aGlsZSAoaW5kZXggPiAwKSB7CiAgICAgIGlmIChudW1baW5kZXggLSAxXSA8IG51bVtpbmRleF0pCiAgICAgICAgYnJlYWs7CiAgICAgIGluZGV4LS07CiAgICB9CiAgICBpZiAoaW5kZXggPT0gMCkgewogICAgICByZXZlcnNlU29ydChudW0sIDAsIG4gLSAxKTsKICAgICAgcmV0dXJuOwogICAgfSBlbHNlIHsKICAgICAgaW50IHZhbCA9IG51bVtpbmRleCAtIDFdOwogICAgICBpbnQgaiA9IG4gLSAxOwogICAgICB3aGlsZSAoaiA+PSBpbmRleCkgewogICAgICAgIGlmIChudW1bal0gPiB2YWwpCiAgICAgICAgICBicmVhazsKICAgICAgICBqLS07CiAgICAgIH0KICAgICAgc3dhcChudW0sIGosIGluZGV4IC0gMSk7CiAgICAgIHJldmVyc2VTb3J0KG51bSwgaW5kZXgsIG4gLSAxKTsKICAgICAgcmV0dXJuOwogICAgfQogIH0KCiAgcHVibGljIHN0YXRpYyB2b2lkIHN3YXAoaW50W10gbnVtLCBpbnQgaSwgaW50IGopIHsKICAgIGludCB0ZW1wID0gMDsKICAgIHRlbXAgPSBudW1baV07CiAgICBudW1baV0gPSBudW1bal07CiAgICBudW1bal0gPSB0ZW1wOwogIH0KCiAgcHVibGljIHN0YXRpYyB2b2lkIHJldmVyc2VTb3J0KGludFtdIG51bSwgaW50IHN0YXJ0LCBpbnQgZW5kKSB7CiAgICBpZiAoc3RhcnQgPiBlbmQpCiAgICAgIHJldHVybjsKICAgIGZvciAoaW50IGkgPSBzdGFydDsgaSA8PSAoZW5kICsgc3RhcnQpIC8gMjsgaSsrKQogICAgICBzd2FwKG51bSwgaSwgc3RhcnQgKyBlbmQgLSBpKTsKICB9CgogIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKICAgIFN0cmluZyBzID0gImFiYyI7CiAgICBpbnRbXSB4ID0gbmV3IGludFtzLmxlbmd0aCgpXTsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgcy5sZW5ndGgoKTsgaSsrKSB7CiAgICAgIHhbaV0gPSBzLmNoYXJBdChpKTsKICAgIH0KICAgIG5leHRQZXJtdXRhdGlvbih4KTsKCiAgICBmb3IgKGludCBpIDogeCkgewogICAgICBTeXN0ZW0ub3V0LnByaW50KChjaGFyKSBpKTsKICAgIH0KICB9Cn0K