Techno Blender
Digitally Yours.

Make the String lexicographically larger

0 31


Improve Article

Save Article

Like Article

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++

#include <bits/stdc++.h>

using namespace std;

  

#define int long long int

  

void lexolarge(string s)

{

    int n = s.size();

  

    

    

    char ch = 'a';

    for (int i = 0; i < n; i++) {

        if (s[i] > ch) {

            ch = s[i];

        }

    }

  

    

    

    bool flag = false;

    for (int i = 0; i < n; i++) {

        if (s[i] == ch && !flag) {

            flag = true;

  

            

            

            for (int j = i; j > 0; j--) {

  

                

                

                if (s[j] > s[j - 1]) {

  

                    

                    

                    swap(s[j], s[j - 1]);

                }

            }

        }

        if (flag) {

            break;

        }

    }

  

    

    cout << s << endl;

}

  

signed main()

{

    string str = "bacatf";

  

    

    lexolarge(str);

  

    return 0;

}

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

Last Updated :
25 Apr, 2023

Like Article

Save Article


Improve Article

Save Article

Like Article

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++

#include <bits/stdc++.h>

using namespace std;

  

#define int long long int

  

void lexolarge(string s)

{

    int n = s.size();

  

    

    

    char ch = 'a';

    for (int i = 0; i < n; i++) {

        if (s[i] > ch) {

            ch = s[i];

        }

    }

  

    

    

    bool flag = false;

    for (int i = 0; i < n; i++) {

        if (s[i] == ch && !flag) {

            flag = true;

  

            

            

            for (int j = i; j > 0; j--) {

  

                

                

                if (s[j] > s[j - 1]) {

  

                    

                    

                    swap(s[j], s[j - 1]);

                }

            }

        }

        if (flag) {

            break;

        }

    }

  

    

    cout << s << endl;

}

  

signed main()

{

    string str = "bacatf";

  

    

    lexolarge(str);

  

    return 0;

}

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

Last Updated :
25 Apr, 2023

Like Article

Save Article

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.
Leave a comment