Given a string S, the task is to find the largest lexicographical string with no more than K consecutive occurrences of an element by either re-arranging or deleting the elements.
Examples:
Input: S = “baccc”
K = 2
Output: Result = “ccbca”
Explanation: Since K=2, a maximum of 2 same characters can be placed consecutively.
No. of ‘c’ = 3.
No. of ‘b’ = 1.
No. of ‘a’ = 1.
Since the largest lexicographical string has to be printed, therefore, the answer is “ccbca”.
Input: S = “xxxxzaz”
K = 3
Output: result = “zzxxxax”
Approach:
- Form a frequency array of size 26, where index i is chosen using (a character in a string – ‘a’).
- Initialize an empty string to store corresponding changes.
- For i=25 to 0, do:
- If frequency at index i is greater than k, then append (i + ‘a’) K-times. Decrease frequency by K at index i. Find the next greatest priority element and append to answer and decrease the frequency at the respective index by 1.
- If frequency at index i is greater than 0 but less than k, then append (i + ‘a’) times its frequency.
- If frequency at index i is 0, then that index cannot be used to form an element and therefore check for the next possible highest priority element.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
string getLargestString(string s,
ll k)
{
vector< int > frequency_array(26, 0);
for ( int i = 0;
i < s.length(); i++) {
frequency_array[s[i] - 'a' ]++;
}
string ans = "" ;
for ( int i = 25; i >= 0;) {
if (frequency_array[i] > k) {
int temp = k;
string st(1, i + 'a' );
while (temp > 0) {
ans += st;
temp--;
}
frequency_array[i] -= k;
int j = i - 1;
while (frequency_array[j]
<= 0
&& j >= 0) {
j--;
}
if (frequency_array[j] > 0
&& j >= 0) {
string str(1, j + 'a' );
ans += str;
frequency_array[j] -= 1;
}
else {
break ;
}
}
else if (frequency_array[i] > 0) {
int temp = frequency_array[i];
frequency_array[i] -= temp;
string st(1, i + 'a' );
while (temp > 0) {
ans += st;
temp--;
}
}
else {
i--;
}
}
return ans;
}
int main()
{
string S = "xxxxzza" ;
int k = 3;
cout << getLargestString(S, k)
<< endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static String getLargestString(String s,
int k)
{
int []frequency_array = new int [ 26 ];
for ( int i = 0 ;
i < s.length(); i++)
{
frequency_array[s.charAt(i) - 'a' ]++;
}
String ans = "" ;
for ( int i = 25 ; i >= 0 😉
{
if (frequency_array[i] > k)
{
int temp = k;
String st = String.valueOf(( char )(i + 'a' ));
while (temp > 0 )
{
ans += st;
temp--;
}
frequency_array[i] -= k;
int j = i - 1 ;
while (frequency_array[j] <= 0 &&
j >= 0 )
{
j--;
}
if (frequency_array[j] > 0 &&
j >= 0 )
{
String str = String.valueOf(( char )(j + 'a' ));
ans += str;
frequency_array[j] -= 1 ;
}
else
{
break ;
}
}
else if (frequency_array[i] > 0 )
{
int temp = frequency_array[i];
frequency_array[i] -= temp;
String st = String.valueOf(( char )(i + 'a' ));
while (temp > 0 )
{
ans += st;
temp--;
}
}
else
{
i--;
}
}
return ans;
}
public static void main(String[] args)
{
String S = "xxxxzza" ;
int k = 3 ;
System.out.print(getLargestString(S, k));
}
}
|
Python3
def getLargestString(s, k):
frequency_array = [ 0 ] * 26
for i in range ( len (s)):
frequency_array[ ord (s[i]) -
ord ( 'a' )] + = 1
ans = ""
i = 25
while i > = 0 :
if (frequency_array[i] > k):
temp = k
st = chr ( i + ord ( 'a' ))
while (temp > 0 ):
ans + = st
temp - = 1
frequency_array[i] - = k
j = i - 1
while (frequency_array[j] < = 0 and
j > = 0 ):
j - = 1
if (frequency_array[j] > 0 and
j > = 0 ):
str1 = chr (j + ord ( 'a' ))
ans + = str1
frequency_array[j] - = 1
else :
break
elif (frequency_array[i] > 0 ):
temp = frequency_array[i]
frequency_array[i] - = temp
st = chr (i + ord ( 'a' ))
while (temp > 0 ):
ans + = st
temp - = 1
else :
i - = 1
return ans
if __name__ = = "__main__" :
S = "xxxxzza"
k = 3
print (getLargestString(S, k))
|
C#
using System;
class GFG{
static String getLargestString(String s,
int k)
{
int []frequency_array = new int [26];
for ( int i = 0; i < s.Length; i++)
{
frequency_array[s[i] - 'a' ]++;
}
String ans = "" ;
for ( int i = 25; i >= 0;)
{
if (frequency_array[i] > k)
{
int temp = k;
String st = String.Join( "" ,
( char )(i + 'a' ));
while (temp > 0)
{
ans += st;
temp--;
}
frequency_array[i] -= k;
int j = i - 1;
while (frequency_array[j] <= 0 &&
j >= 0)
{
j--;
}
if (frequency_array[j] > 0 &&
j >= 0)
{
String str = String.Join( "" ,
( char )(j + 'a' ));
ans += str;
frequency_array[j] -= 1;
}
else
{
break ;
}
}
else if (frequency_array[i] > 0)
{
int temp = frequency_array[i];
frequency_array[i] -= temp;
String st = String.Join( "" ,
( char )(i + 'a' ));
while (temp > 0)
{
ans += st;
temp--;
}
}
else
{
i--;
}
}
return ans;
}
public static void Main(String[] args)
{
String S = "xxxxzza" ;
int k = 3;
Console.Write(getLargestString(S, k));
}
}
|
Javascript
<script>
function getLargestString(s,k)
{
let frequency_array = new Array(26);
for (let i=0;i<26;i++)
{
frequency_array[i]=0;
}
for (let i = 0;
i < s.length; i++)
{
frequency_array[s[i].charCodeAt(0) - 'a' .charCodeAt(0)]++;
}
let ans = "" ;
for (let i = 25; i >= 0;)
{
if (frequency_array[i] > k)
{
let temp = k;
let st = String.fromCharCode(i + 'a' .charCodeAt(0));
while (temp > 0)
{
ans += st;
temp--;
}
frequency_array[i] -= k;
let j = i - 1;
while (frequency_array[j] <= 0 &&
j >= 0)
{
j--;
}
if (frequency_array[j] > 0 &&
j >= 0)
{
let str = String.fromCharCode(j + 'a' .charCodeAt(0));
ans += str;
frequency_array[j] -= 1;
}
else
{
break ;
}
}
else if (frequency_array[i] > 0)
{
let temp = frequency_array[i];
frequency_array[i] -= temp;
let st = String.fromCharCode(i + 'a'.charCodeAt(0));
while (temp > 0)
{
ans += st;
temp--;
}
}
else
{
i--;
}
}
return ans;
}
let S = "xxxxzza" ;
let k = 3;
document.write(getLargestString(S, k));
</script>
|
Time Complexity: O(N), where N is the length of the given string.
Auxiliary Space: O(N)
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!