• + 0 comments

    c++ solution, disliking the fact that I spent a bit too long thinking about how to do this, no cause the problem explenation sucks.

    string biggerIsGreater(string w) {
        int n = w.length();
        int i = n - 2;
    
        while (i >= 0 && w[i] >= w[i + 1]) {
            i--;
        }
    
        if (i < 0) return "no answer"; 
    
        int j = n - 1;
        while (w[j] <= w[i]) {
            j--;
        }
    
        swap(w[i], w[j]);
    
        reverse(w.begin() + i + 1, w.end());
    
        return w;
    }