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.
Uff, I'm not following your logic, but if you tried the same of yerzhik, the trick is, the "j" is the position you need the change, but is not a simple swap, first you need to Order the letters from j to Zise-1, and then swap by the firs char greather than chars[j]
code from yershik:
for (int j = len - 2; j >= 0; --j) {
if (chars[j] < chars[j + 1]) {
ind = j;
break;
}
}
sample:
POS: 012345678
WORD:acfhomkba
ind = 3 (h)
then: order from 4 to 8 => [omkba]->[abkmo]
=> acf h abkmo
then: swap chars[ind] by FIRST letter[ind to N-1] where chars[ind]>letter , => swap h by k
RESULT => acfkabhmo
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 →
Uff, I'm not following your logic, but if you tried the same of yerzhik, the trick is, the "j" is the position you need the change, but is not a simple swap, first you need to Order the letters from j to Zise-1, and then swap by the firs char greather than chars[j]
code from yershik: for (int j = len - 2; j >= 0; --j) { if (chars[j] < chars[j + 1]) { ind = j; break; } }
sample: POS: 012345678 WORD:acfhomkba ind = 3 (h) then: order from 4 to 8 => [omkba]->[abkmo] => acf h abkmo then: swap chars[ind] by FIRST letter[ind to N-1] where chars[ind]>letter , => swap h by k RESULT => acfkabhmo