• + 0 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++; }

    if (decStart == -1) {
        return "no answer";
    }
    
    string descending = w.substr(decStart);
    reverse(descending.begin() + 1, descending.end());
    if (swapIndex + 1 < decStart) {
        reverse(w.begin() + swapIndex + 1, w.begin() + decStart);
    }
    w.erase(decStart);
    w.insert(swapIndex, descending);
    return w;`
    

    }