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++
#include <bits/stdc++.h>
using namespace std;
#define int long long int
void lexolarge(string s , string tmp)
{
int n = s.size();
sort(tmp.rbegin(),tmp.rend());
int i = 0;
while (i<n and s[i]==tmp[i]) ++i;
int start = i;
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;
}
signed main()
{
string str = "baba" ;
string tmp = str;
lexolarge(str,tmp);
str = "zyxbacaztf" ;
tmp = str;
lexolarge(str,tmp);
return 0;
}
|
Java
import java.util.Arrays;
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);
}
}
|
Python3
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)
|
C#
using System;
public class Program
{
public static void LexoLarge( string s, string tmp)
{
int n = s.Length;
char [] tmpArr = tmp.ToCharArray();
Array.Sort(tmpArr);
Array.Reverse(tmpArr);
tmp = new string (tmpArr);
int i = 0;
while (i < n && s[i] == tmp[i])
{
i++;
}
int start = i;
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);
}
public static void Main()
{
string str = "baba" ;
string tmp = str;
LexoLarge(str, tmp);
str = "zyxbacaztf" ;
tmp = str;
LexoLarge(str, tmp);
}
}
|
Javascript
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);
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!