Bigger is Greater

Lexicographical order is often known as alphabetical order when dealing with strings. A string is greater than another string if it comes later in a lexicographically sorted list.

Given a word, create a new word by swapping some or all of its characters. This new word must meet two criteria:

  • It must be greater than the original word
  • It must be the smallest word that meets the first condition

Example:

Input:  w = "hefg"
Output: hegf

Approach

C++

#include <bits/stdc++.h>
using namespace std;

string biggerIsGreater(string w)
{
    if (next_permutation(w.begin(), w.end()))
        return w;
    return "no answer";
}

int main()
{

    string w = "hefg";

    cout << biggerIsGreater(w<< "\n";

    return 0;
}


No comments:

Post a Comment