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 till where the string is already sorted (in decreasing order)
- Find the greatest character, ch, in the remaining string s.
- concatinate the strings -> already sorted substring + max_character_in_remaining_string + left side substring of max character in remaining substring + right side of the max character in remaining sub string .
-
ex-> str = “zyxbacatfz”
- already sorted string sor_str = “z” ( l = 0 to r = 0 )
- remaining string rem_str = “yxbacaztf”
- max_char in rem_str = ‘z’ (at i = 7)
- answer string ans = sor_str + max_char + “yxbaca” + “tf” ;
Below are the steps involved in the implementation of the code:
// 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 , string tmp)
{ int n = s.size();
// sorting the temperary string in decreasing order
sort(tmp.rbegin(),tmp.rend());
// finding the index where we have to put the largest character
int i = 0;
while (i<n and s[i]==tmp[i]) ++i;
int start = i;
// Find the largest character
// in the string
char ch = 'a' ;
int idx = -1;
while (i<n) {
if (s[i] >= ch) {
ch = s[i];
idx = i;
}
++i;
}
string ans = s.substr(0,start) + ch + s.substr(start,idx-start) + (idx+1<n?s.substr(idx+1): "" );
cout << ans << endl;
} // Driver code signed main()
{ // first test case
string str = "baba" ;
string tmp = str;
// Function call
lexolarge(str,tmp);
// second test case
str = "zyxbacaztf" ;
tmp = str;
// Function call
lexolarge(str,tmp);
return 0;
} |
import java.util.Arrays;
// Nikunj Sonigara public class GFG {
static void lexolarge(String s, String tmp) {
int n = s.length();
char [] tmpArray = tmp.toCharArray();
Arrays.sort(tmpArray);
tmp = new String(tmpArray);
int i = 0 ;
while (i < n && s.charAt(i) == tmp.charAt(i)) {
i++;
}
int start = i;
char ch = 'a' ;
int idx = - 1 ;
while (i < n) {
if (s.charAt(i) >= ch) {
ch = s.charAt(i);
idx = i;
}
i++;
}
StringBuilder ans = new StringBuilder(s.substring( 0 , start) + ch + s.substring(start, idx));
if (idx + 1 < n) {
ans.append(s.substring(idx + 1 ));
}
System.out.println(ans);
}
public static void main(String[] args) {
String str1 = "baba" ;
String tmp1 = str1;
lexolarge(str1, tmp1);
String str2 = "zyxbacaztf" ;
String tmp2 = str2;
lexolarge(str2, tmp2);
}
} |
def lexolarge(s, tmp):
n = len (s)
tmp = ''.join( sorted (tmp, reverse = True ))
i = 0
while i < n and s[i] = = tmp[i]:
i + = 1
start = i
ch = 'a'
idx = - 1
while i < n:
if s[i] > = ch:
ch = s[i]
idx = i
i + = 1
ans = s[:start] + ch + s[start:idx] + (s[idx + 1 :] if idx + 1 < n else '')
print (ans)
str = "baba"
tmp = str
lexolarge( str , tmp)
str = "zyxbacaztf"
tmp = str
lexolarge( str , tmp)
|
using System;
public class Program
{ // Function to create lexographic largest
// string
public static void LexoLarge( string s, string tmp)
{
int n = s.Length;
char [] tmpArr = tmp.ToCharArray();
// sorting the temperary string in decreasing order
Array.Sort(tmpArr);
Array.Reverse(tmpArr);
tmp = new string (tmpArr);
// finding the index where we have to put the largest character
int i = 0;
while (i < n && s[i] == tmp[i])
{
i++;
}
int start = i;
// Find the largest character
// in the string
char ch = 'a' ;
int idx = -1;
while (i < n)
{
if (s[i] >= ch)
{
ch = s[i];
idx = i;
}
i++;
}
string ans = s.Substring(0, start) + ch + s.Substring(start, idx - start) + (idx + 1 < n ? s.Substring(idx + 1) : "" );
Console.WriteLine(ans);
}
// Driver code
public static void Main()
{
string str = "baba" ;
string tmp = str;
LexoLarge(str, tmp);
str = "zyxbacaztf" ;
tmp = str;
LexoLarge(str, tmp);
}
} |
function lexolarge(s, tmp) {
let n = s.length;
tmp = tmp.split( '' ).sort().reverse().join( '' );
let i = 0;
while (i < n && s[i] === tmp[i]) {
i++;
}
let start = i;
let ch = 'a' ;
let idx = -1;
while (i < n) {
if (s[i] >= ch) {
ch = s[i];
idx = i;
}
i++;
}
let ans = s.substring(0, start) + ch + s.substring(start, idx) + (idx + 1 < n ? s.substring(idx + 1) : '' );
console.log(ans);
} let str = "baba" ;
let tmp = str; lexolarge(str, tmp); str = "zyxbacaztf" ;
tmp = str; lexolarge(str, tmp); |
bbaa zzyxbacatf
Time Complexity: O(N)
Auxiliary Space: O(1)