Maximize length of subsequence consisting of single distinct character possible by K increments in a string


In this problem, we need to find the maximum length of the subsequence containing the single character by increasing the multiple characters by, at most, k times.

We can use the sliding window approach to solve the problem. We can find the maximum length of any window to get the result after sorting the string.

Problem statement – We have given string str containing lowercase alphabetical characters. Also, we have given the positive integer k. After performing the at most k increment operations with multiple characters of the given string, we need to find the maximum length of the subsequence containing the single character.

Sample examples

Input– str = "acccdera", k = 5

Output– 5

Explanation– We can increase the value of ‘a’ to ‘c’, which will require 4 increment operations. So, the resultant string will be ‘ccccderc’, which contains the subsequence of ‘c’ of length 5.

Input– str = ‘agkt’, k = 1

Output– 1

Explanation– We need to choose any single character as a subsequence as we can’t get multiple same digits by increasing any character by 1.

Input– str = ‘hijjjkdfr’, k = 3

Output– 5

Explanation– We can increase ‘h’ by 2 and ‘I’ by 1. So, the resultant string will be ‘jjjjjkdfr’, which contains 5 ‘j’.

Approach 1

In this approach, first, we will sort the string. As we need to find the subsequence length of a single character, it will become easy for us to use the sliding window approach after sorting the string.

Algorithm

  • Store the length of the string in the ‘len’ variable.

  • Use the sort() method to sort the string.

  • Initialize the ‘start’ and ‘end’ variables with zero for the sliding window pointers. Also, initialize the ‘max_len’ with zero to store the maximum length of subsequence and ‘sum’ with zero to store the sum of all sliding window characters.

  • Start traversing the sorted string.

  • Add the current character’s ASCII value to the sum.

  • Make iterations using while loop until sum + K < (str[end] - 'a') * (end - start + 1) is true. Here, ‘(str[end] - 'a') * (end - start + 1)’ represents the character value multiplied by the size of the sliding window.

  • In the loop, subtract the character value of the character at the start position. Also, increase the value of the start by 1.

  • In the max_len variable, store the maximum from max_len, and end - start + 1 (sliding window size).

  • Return the max_len value.

Example

#include <bits/stdc++.h>
using namespace std;
// function to find the maximum length of the subsequence such that we get it after at most k increment operations
int getSubSeqLen(string str, int K){
   // Store the size of str
   int len = str.length();
   // Sort the given string
   sort(str.begin(), str.end());
   // variables for sliding window
   int start = 0, end = 0;
   // to store the maximum length of subsequence and the sum of ASCII values of characters in the sliding window.
   int max_len = 0, sum = 0;
   // Traverse the string str
   for (end = 0; end < len; end++){
      // Add the ASCII value of the current character in the sum
      sum = sum + (str[end] - 'a');
      // Decrease the window size by removing the leftmost characters
      while (sum + K < (str[end] - 'a') * (end - start + 1)){
          // remove the leftmost character from the sum
          sum = sum - (str[start] - 'a');
          // Increase the start index
          start++;
      }
      // Update the maximum length according to the current window size
      max_len = max(max_len, end - start + 1);
   }
   // return the maximum length of the subsequence
   return max_len;
}
int main(){
   string str = "acccdera";
   int K = 5;
   cout << "The maximum length of the subsequence of the same character after performing at most k operations is " << getSubSeqLen(str, K);
   return 0;
}

Output

The maximum length of the subsequence of the same character after performing at most k operations is 5

Time complexity – O(N*logN + N) ~ O(N*logN). Here, the time complexity of the sorting function is O(NlogN), and the sliding window approach is O(N).

Space complexity – O(1), as we don’t use any extra space.

In the above code, we find the window which contains different characters, and it is possible to make them the same as the last character of the window by performing the total at most k increments. After that, we find the maximum length of such windows using the sliding window approach.

Updated on: 18-Aug-2023

106 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements