The Wayback Machine - https://web.archive.org/web/20230503164409/https://www.geeksforgeeks.org/make-the-string-lexicographically-larger/
Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Make the String lexicographically larger

Improve Article
Save Article
Like Article
author
kg_codex
contributor
14 published articles
Improve Article
Save Article
Like Article

Given a string str of length n. The task is to find a lexicographic largest string where you are allowed to shift one character from str to any other index only one time.

Examples:

Input: n = 3, str = “bac”
Output: “cba”
Explanation: We can perform the given operation exactly one time on the string str and make many strings. Among them “cba” is lexicographically greater.

Input: n = 6, str = “bacatf”
Output: tbacaf

 Approach: This can be solved with the following idea:

The approach finds the leftmost occurrence of the greatest character and swaps it with the character immediately to its left if it is lesser than the greatest element. This operation ensures that the resulting string is lexicographically larger than the original string. The approach uses the fact that swapping two adjacent characters in a string can only increase its lexicographic order if the character on the left is greater than the character on the right.

Steps involved in the implementation of the code:

  • Read the input values of the length of the string, n, and the string itself, str.
  • Find the greatest character, ch, in the string s.
  • Traverse the string s from left to right and find the leftmost occurrence of the greatest character ch in s.
  • If the leftmost occurrence is at the beginning of the string, the string is already lexicographically largest, and no operation is required. In this case, print the string s and move to the next test case.
  • Iterate over the string and find where the greatest character appears first from the left from there we can travel to the beginning and swap the letter when the character just left of the ch is lesser than it, either break the loop and return the string.

Below are the steps involved in the implementation of the code:

C++




// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
  
#define int long long int
  
// Function to create lexographic largest
// string
void lexolarge(string s)
{
    int n = s.size();
  
    // Find the largest character
    // in the string
    char ch = 'a';
    for (int i = 0; i < n; i++) {
        if (s[i] > ch) {
            ch = s[i];
        }
    }
  
    // Move the largest character to the
    // beginning of the string
    bool flag = false;
    for (int i = 0; i < n; i++) {
        if (s[i] == ch && !flag) {
            flag = true;
  
            // Moving to the beginning
            // by swaping
            for (int j = i; j > 0; j--) {
  
                // If the next character
                // is lesser
                if (s[j] > s[j - 1]) {
  
                    // Swaping the
                    // character
                    swap(s[j], s[j - 1]);
                }
            }
        }
        if (flag) {
            break;
        }
    }
  
    // Print the result
    cout << s << endl;
}
  
// Driver code
signed main()
{
    string str = "bacatf";
  
    // Function call
    lexolarge(str);
  
    return 0;
}

Output

cba

Time Complexity: O(N)
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Last Updated : 25 Apr, 2023
Like Article
Save Article
Similar Reads
Related Tutorials