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.
`string biggerIsGreater(string w) {
int index = 0;
int decStart = -1;
int swapIndex = -1;
while (index < w.length()) {
// Increasing
if (index + 1 < w.length() && w[index] < w[index + 1]) {
decStart = index + 1;
swapIndex = index;
}
// Decreasing, we found a new minimum that is smaller
else if (decStart != -1 && w[index] <= w[decStart]
&& w[index] > w[swapIndex]) {
// If the new character is greater than the character to be swapping
decStart = index;
}
index++;
}
Bigger is Greater
You are viewing a single comment's thread. Return to all comments →
`string biggerIsGreater(string w) { int index = 0; int decStart = -1; int swapIndex = -1; while (index < w.length()) { // Increasing if (index + 1 < w.length() && w[index] < w[index + 1]) { decStart = index + 1; swapIndex = index; } // Decreasing, we found a new minimum that is smaller else if (decStart != -1 && w[index] <= w[decStart] && w[index] > w[swapIndex]) { // If the new character is greater than the character to be swapping decStart = index; } index++; }
}