Maximize length of subsequence consisting of single distinct character possible by K increments in a string
Last Updated :
04 Aug, 2022
Given a string S consisting of lowercase characters and an integer K, the task is to find the maximum length of a subsequence consisting of a single distinct character possible by incrementing at most K characters.
Examples:
Input: S = “acscbcca” K = 1
Output: 5
Explanation: Incrementing the character S[4] from ‘b’ to ‘c’, modifies the string to “acscccca”. Therefore, longest subsequence of same characters is “ccccc”. Length of the subsequence is 5.
Input: S = “adscassr”, K = 2
Output: 4
Approach: This given problem can be solved by using the Sliding window technique and Sorting. Follow the steps to solve this problem:
- Initialize the variables, say start, end, and sum to 0 that stores the starting and ending indexes of the subsequences the sum of the sliding window.
- Initialize a variable, say ans to INT_MIN that stores the length of the resultant longest subsequence.
- Sort the given string S.
- Traverse the string over the range [0, N – 1] and perform the following steps:
- Increment the value of the sum by the value (S[end] – ‘a’).
- Iterate a loop until the value of (sum + K) is less than (S[end] – ‘a’) * (end – start + 1) and perform the following steps:
- Decrement the value of the sum by (S[start] – ‘a’).
- Increment the value of the start by 1.
- After the above steps, all the characters over the range [start, end] can be made equal by using at most K increments. Therefore, update the value of ans as the maximum of ans and (end – start + 1).
- After completing the above steps, print the value of ans as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void maxSubsequenceLen(string S, int K)
{
int N = S.length();
int start = 0, end = 0;
sort(S.begin(), S.end());
int ans = INT_MIN, sum = 0;
for (end = 0; end < N; end++) {
sum = sum + (S[end] - 'a' );
while (sum + K
< (S[end] - 'a' ) * (end - start + 1)) {
sum = sum - (S[start] - 'a' );
start++;
}
ans = max(ans, end - start + 1);
}
cout << ans;
}
int main()
{
string S = "acscbcca" ;
int K = 1;
maxSubsequenceLen(S, K);
return 0;
}
|
Java
import java.io.*;
import java.math.*;
import java.util.Arrays;
class GFG{
static void maxSubsequenceLen(String s, int K)
{
int N = s.length();
int start = 0 , end = 0 ;
char S[] = s.toCharArray();
Arrays.sort(S);
int ans = Integer.MIN_VALUE, sum = 0 ;
for (end = 0 ; end < N; end++)
{
sum = sum + (S[end] - 'a' );
while (sum + K < (S[end] - 'a' ) *
(end - start + 1 ))
{
sum = sum - (S[start] - 'a' );
start++;
}
ans = Math.max(ans, end - start + 1 );
}
System.out.println(ans);
}
public static void main(String args[])
{
String S = "acscbcca" ;
int K = 1 ;
maxSubsequenceLen(S, K);
}
}
|
Python3
def maxSubsequenceLen(S, K):
N = len (S)
start, end = 0 , 0
S = sorted (S)
ans, sum = - 10 * * 9 , 0
for end in range (N):
sum = sum + ( ord (S[end]) - ord ( 'a' ))
while ( sum + K < ( ord (S[end]) - ord ( 'a' )) *
(end - start + 1 )):
sum = sum - ( ord (S[start]) - ord ( 'a' ))
start + = 1
ans = max (ans, end - start + 1 )
print (ans)
if __name__ = = '__main__' :
S = "acscbcca"
K = 1
maxSubsequenceLen(S, K)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void maxSubsequenceLen( string s, int K)
{
int N = s.Length;
int start = 0, end = 0;
char [] S= s.ToCharArray();
Array.Sort(S);
int ans = Int32.MinValue, sum = 0;
for (end = 0; end < N; end++)
{
sum = sum + (S[end] - 'a' );
while (sum + K < (S[end] - 'a' ) *
(end - start + 1))
{
sum = sum - (S[start] - 'a' );
start++;
}
ans = Math.Max(ans, end - start + 1);
}
Console.WriteLine(ans);
}
public static void Main()
{
string S = "acscbcca" ;
int K = 1;
maxSubsequenceLen(S, K);
}
}
|
Javascript
<script>
function maxSubsequenceLen(s, K)
{
var N = s.length;
var start = 0, end = 0;
var S = s.split( '' );
S.sort();
var ans = Number.MIN_VALUE, sum = 0;
for (end = 0; end < N; end++)
{
sum = sum + (S[end].charCodeAt(0) -
'a' .charCodeAt(0));
while (sum + K < (S[end].charCodeAt(0) -
'a' .charCodeAt(0)) *
(end - start + 1))
{
sum = sum - (S[start].charCodeAt(0) -
'a' .charCodeAt(0));
start++;
}
ans = Math.max(ans, end - start + 1);
}
document.write(ans);
}
var S = "acscbcca" ;
var K = 1;
maxSubsequenceLen(S, K);
</script>
|
Time Complexity: O(N * log N), only one traversal of string takes O(n) time and sorting them takes extra log N time and thus the overall time complexity turns out to be O( N log N)
Auxiliary Space: O(1), since no extra array is used so it has constant space
Share your thoughts in the comments
Please Login to comment...