Maximum number of removals of given subsequence from a string
Given a string str, the task is to count the maximum number of possible operations that can be performed on str. An operation consists of taking a sub-sequence ‘gks’ from the string and removing it from the string.
Examples:
Input: str = "ggkssk" Output: 1 After 1st operation: str = "gsk" No further operation can be performed. Input: str = "kgs" Output: 0
Approach:
- Take three variables g, gk and gks which will store the occurrence of the sub-sequences ‘g’, ‘gk’ and ‘gks’ respectively.
- Traverse the string character by character:
- If str[i] = ‘g’ then update g = g + 1.
- If str[i] = ‘k’ and g > 0 then update g = g – 1 and gk = gk + 1 as previously found ‘g’ now contributes in the sub-sequence ‘gk’ along with the current ‘k’.
- Similarly, if str[i] = ‘s’ and gk > 0 then update gk = gk – 1 and gks = gks + 1.
- Print the value of gks in the end.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return max possible operation // of the given type that can be performed on str int maxOperations(string str) { int i, g, gk, gks; i = g = gk = gks = 0; for (i = 0; i < str.length(); i++) { if (str[i] == 'g') { // Increment count of sub-sequence 'g' g++; } else if (str[i] == 'k') { // Increment count of sub-sequence 'gk' // if 'g' is available if (g > 0) { g--; gk++; } } else if (str[i] == 's') { // Increment count of sub-sequence 'gks' // if sub-sequence 'gk' appeared previously if (gk > 0) { gk--; gks++; } } } // Return the count of sub-sequence 'gks' return gks; } // Driver code int main() { string a = "ggkssk"; cout << maxOperations(a); return 0; } |
chevron_right
filter_none
Java
// Java implementation of the approach class GFG { // Function to return max possible // operation of the given type that // can be performed on str static int maxOperations(String str) { int i, g, gk, gks; i = g = gk = gks = 0; for (i = 0; i < str.length(); i++) { if (str.charAt(i) == 'g') { // Increment count of sub-sequence 'g' g++; } else if (str.charAt(i) == 'k') { // Increment count of sub-sequence 'gk' // if 'g' is available if (g > 0) { g--; gk++; } } else if (str.charAt(i) == 's') { // Increment count of sub-sequence 'gks' // if sub-sequence 'gk' appeared previously if (gk > 0) { gk--; gks++; } } } // Return the count of sub-sequence 'gks' return gks; } // Driver code public static void main(String args[]) { String a = "ggkssk"; System.out.print(maxOperations(a)); } } // This code is contributed // by Akanksha Rai |
chevron_right
filter_none
Python 3
# Python 3 implementation of the approach # Function to return max possible operation # of the given type that can be performed # on str def maxOperations( str): i, g, gk, gks = 0, 0, 0, 0 for i in range(len(str)) : if (str[i] == 'g') : # Increment count of sub-sequence 'g' g += 1 elif (str[i] == 'k') : # Increment count of sub-sequence # 'gk', if 'g' is available if (g > 0) : g -= 1 gk += 1 elif (str[i] == 's') : # Increment count of sub-sequence 'gks' # if sub-sequence 'gk' appeared previously if (gk > 0) : gk -= 1 gks += 1 # Return the count of sub-sequence 'gks' return gks # Driver code if __name__ == "__main__": a = "ggkssk" print(maxOperations(a)) # This code is contributed by ita_c |
chevron_right
filter_none
C#
// C# implementation of the approach using System ; public class GFG{ // Function to return max possible operation // of the given type that can be performed on str static int maxOperations(string str) { int i, g, gk, gks; i = g = gk = gks = 0; for (i = 0; i < str.Length; i++) { if (str[i] == 'g') { // Increment count of sub-sequence 'g' g++; } else if (str[i] == 'k') { // Increment count of sub-sequence 'gk' // if 'g' is available if (g > 0) { g--; gk++; } } else if (str[i] == 's') { // Increment count of sub-sequence 'gks' // if sub-sequence 'gk' appeared previously if (gk > 0) { gk--; gks++; } } } // Return the count of sub-sequence 'gks' return gks; } // Driver code public static void Main() { string a = "ggkssk"; Console.WriteLine(maxOperations(a)) ; } } |
chevron_right
filter_none
PHP
<?php // PHP implementation of the approach // Function to return max possible operation // of the given type that can be performed on str function maxOperations($str) { $i = $g = $gk = $gks = 0; for ($i = 0; $i < strlen($str); $i++) { if ($str[$i] == 'g') { // Increment count of sub-sequence 'g' $g++; } else if ($str[$i] == 'k') { // Increment count of sub-sequence 'gk' // if 'g' is available if ($g > 0) { $g--; $gk++; } } else if ($str[$i] == 's') { // Increment count of sub-sequence 'gks' // if sub-sequence 'gk' appeared previously if ($gk > 0) { $gk--; $gks++; } } } // Return the count of sub-sequence 'gks' return $gks; } // Driver code $a = "ggkssk"; echo maxOperations($a); // This code is contributed // by Akanksha Rai ?> |
chevron_right
filter_none
Output:
1
Recommended Posts:
- Maximum length prefix of one string that occurs as subsequence in another
- Find number of times a string occurs as a subsequence in given string
- Find length of longest subsequence of one string which is substring of another string
- Maximum number of characters between any two same character in a string
- Find the maximum possible Binary Number from given string
- String with maximum number of unique characters
- Maximum number of given operations to remove the entire string
- Maximum even sum subsequence
- Subsequence with maximum odd sum
- Maximum Sum Subsequence of length k
- Maximum subsequence sum such that all elements are K distance apart
- Maximum length subsequence possible of the form R^N K^N
- Printing Maximum Sum Increasing Subsequence
- Maximum product of subsequence of size k
- Maximum Bitwise AND value of subsequence of length K
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

