Lexicographically largest string possible consisting of at most K consecutive similar characters
Given a string S and an integer K, the task is to generate lexicographically the largest string possible from the given string, by removing characters also, that consist of at most K consecutive similar characters.
Examples:
Input: S = “baccc”, K = 2
Output: ccbca
Input: S = “ccbbb”, K = 2
Output: ccbb
Approach: Follow the steps below to solve the problem:
- Initialize an array charset[] to store the frequency of each character in the string.
- Traverse the string and store the frequency of each character in the array.
- Initialize a variable count to store the count of similar consecutive characters
- Initialize a string newString to store the resultant string.
- Traverse the array charset[] and append (i +’a’) to newString.
- Decrease charset[i] and increase count.
- Check if count = K and charset[i] > 0, then find the nearest smaller character available from charset[] and append to newString. If the nearest smaller character is not available, then print newString
- Otherwise, reset count to 0.
- Repeat steps from 2 to 5 until charset[i] > 0
- Finally, return newString.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
char nextAvailableChar(vector< int > charset,
int start)
{
for ( int i = start - 1; i >= 0; i--)
{
if (charset[i] > 0)
{
charset[i]--;
return char (i + 'a' );
}
}
return '\0' ;
}
string newString(string originalLabel,
int limit)
{
int n = originalLabel.length();
vector< int > charset(26, 0);
string newStrings = "" ;
for ( char i : originalLabel)
charset[i - 'a' ]++;
for ( int i = 25; i >= 0; i--)
{
int count = 0;
while (charset[i] > 0)
{
newStrings += char (i + 'a' );
charset[i]--;
count++;
if (charset[i] > 0 &&
count == limit)
{
char next = nextAvailableChar(charset, i);
if (next == '\0' )
return newStrings;
newStrings += next;
count = 0;
}
}
}
return newStrings;
}
int main()
{
string S = "ccbbb" ;
int K = 2;
cout << (newString(S, K));
}
|
Java
import java.util.*;
class GFG {
static String newString(String originalLabel,
int limit)
{
int n = originalLabel.length();
int [] charset = new int [ 26 ];
for ( int i = 0 ; i < n; i++) {
charset[originalLabel.charAt(i) - 'a' ]++;
}
StringBuilder newString
= new StringBuilder(n);
for ( int i = 25 ; i >= 0 ; i--) {
int count = 0 ;
while (charset[i] > 0 ) {
newString.append(( char )(i + 'a' ));
charset[i]--;
count++;
if (charset[i] > 0 && count == limit) {
Character next
= nextAvailableChar(charset, i);
if (next == null )
return newString.toString();
newString.append(next);
count = 0 ;
}
}
}
return newString.toString();
}
static Character nextAvailableChar( int [] charset,
int start)
{
for ( int i = start - 1 ; i >= 0 ; i--) {
if (charset[i] > 0 ) {
charset[i]--;
return ( char )(i + 'a' );
}
}
return null ;
}
public static void main(String[] args)
{
String S = "ccbbb" ;
int K = 2 ;
System.out.println(newString(S, K));
}
}
|
Python3
def nextAvailableChar(charset,
start):
for i in range (start - 1 ,
- 1 , - 1 ):
if (charset[i] > 0 ):
charset[i] - = 1
return chr (i + ord ( 'a' ))
return '\0'
def newString(originalLabel,
limit):
n = len (originalLabel)
charset = [ 0 ] * ( 26 )
newStrings = ""
for i in originalLabel:
charset[ ord (i) -
ord ( 'a' )] + = 1
for i in range ( 25 , - 1 , - 1 ):
count = 0
while (charset[i] > 0 ):
newStrings + = chr (i + ord ( 'a' ))
charset[i] - = 1
count + = 1
if (charset[i] > 0 and
count = = limit):
next = nextAvailableChar(charset, i)
if ( next = = '\0' ):
return newStrings
newStrings + = next
count = 0
return newStrings
if __name__ = = "__main__" :
S = "ccbbb"
K = 2
print (newString(S, K))
|
C#
using System;
using System.Text;
class GFG{
static String newString(String originalLabel,
int limit)
{
int n = originalLabel.Length;
int [] charset = new int [26];
for ( int i = 0; i < n; i++)
{
charset[originalLabel[i] - 'a' ]++;
}
StringBuilder newString =
new StringBuilder(n);
for ( int i = 25; i >= 0; i--)
{
int count = 0;
while (charset[i] > 0)
{
newString.Append(( char )(i + 'a' ));
charset[i]--;
count++;
if (charset[i] > 0 &&
count == limit)
{
char next =
nextAvailableChar(charset, i);
if (next == 0)
return newString.ToString();
newString.Append(next);
count = 0;
}
}
}
return newString.ToString();
}
static char nextAvailableChar( int [] charset,
int start)
{
for ( int i = start - 1; i >= 0; i--)
{
if (charset[i] > 0)
{
charset[i]--;
return ( char )(i + 'a' );
}
}
return '\0' ;
}
public static void Main(String[] args)
{
String S = "ccbbb" ;
int K = 2;
Console.WriteLine(
newString(S, K));
}
}
|
Javascript
<script>
function newString(originalLabel,limit)
{
let n = originalLabel.length;
let charset = new Array(26);
for (let i = 0; i < 26; i++)
{
charset[i] = 0;
}
for (let i = 0; i < n; i++) {
charset[originalLabel[i].charCodeAt(0) - 'a' .charCodeAt(0)]++;
}
let newString = [];
for (let i = 25; i >= 0; i--) {
let count = 0;
while (charset[i] > 0) {
newString.push(String.fromCharCode(i + 'a' .charCodeAt(0)));
charset[i]--;
count++;
if (charset[i] > 0 && count == limit) {
let next
= nextAvailableChar(charset, i);
if (next == null )
return newString.join( "" );
newString.push(next);
count = 0;
}
}
}
return newString.join( "" );
}
function nextAvailableChar(charset,start)
{
for (let i = start - 1; i >= 0; i--) {
if (charset[i] > 0) {
charset[i]--;
return String.fromCharCode(i + 'a' .charCodeAt(0));
}
}
return null ;
}
let S = "ccbbb" ;
let K = 2;
document.write(newString(S, K));
</script>
|
Time Complexity: O(N), where N is the length of the given string
Auxiliary Space: O(N)
Last Updated :
28 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...