Given a string S, print those permutations of string S which are lexicographically greater than S. If there is no such permutation of string, print -1.
Examples:
Input : BCA
Output : CAB, CBA
Explanation:
Here, S = “BCA”, and there are 2 strings “CAB, CBA” which are lexicographically greater than S.Input : CBA
Output : -1
There is no string which is lexicographically greater than S, so the output is -1.
Approach: To solve the problem mentioned above we will use STL. Use next_permuation() and prev_permutation() functions to check and the lexicographically greater strings. If the string is greater then print it otherwise print -1.
Below is the implementation of above approach:
// C++ program to print the lexicographically // greater strings then the given string #include <bits/stdc++.h> using namespace std; // Function to print the lexicographically // greater strings then the given string void print_lexiStrings(string S) { // Condition to check if there is no // string which is lexicographically // greater than string S if (!next_permutation(S.begin(), S.end())) cout << "-1"; // Move to the previous permutation prev_permutation(S.begin(), S.end()); // Iterate over all the // lexicographically greater strings while (next_permutation(S.begin(), S.end())) { cout << S << "\n"; } } // Driver Code int main() { string S = "ABC"; print_lexiStrings(S); } |
ACB BAC BCA CAB CBA
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Print all permutations of a number N greater than itself
- Print all the combinations of a string in lexicographical order
- Print all palindrome permutations of a string
- Print all permutations of a string in Java
- Write a program to print all permutations of a given string
- Print all distinct permutations of a given string with duplicates
- Print first n distinct permutations of string using itertools in Python
- Java Program to print distinct permutations of a string
- Print all the palindromic permutations of given string in alphabetic order
- Print all longest common sub-sequences in lexicographical order
- Print all distinct circular strings of length M in lexicographical order
- K-th lexicographical string of given length
- Lexicographical concatenation of all substrings of a string
- Lexicographical Maximum substring of string
- Find the k-th string in lexicographical order consisting of n-2 X's and 2 Y's
- Largest lexicographical string with at most K consecutive elements
- Print the two possible permutations from a given sequence
- Print k different sorted permutations of a given array
- Print all permutations with repetition of characters
- Find Kth largest string from the permutations of the string with two characters
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

