Maximum number of removals of given subsequence from a string Last Updated : 03 Oct, 2022 Comments Improve Suggest changes Like Article Like Report Given 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: 1Explanation: 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 to 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; } 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 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 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)) ; } } 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 ?> JavaScript <script> // Javascript implementation of the approach // Function to return max possible // operation of the given type that // can be performed on str function maxOperations(str) { let 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 let a = "ggkssk"; document.write(maxOperations(a)); // This code is contributed by avanitrachhadiya2155 </script> Output1 Time Complexity: O(n)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Maximum number of removals of given subsequence from a string S sahilshelangia Follow Improve Article Tags : Misc Strings DSA subsequence frequency-counting +1 More Practice Tags : MiscStrings Similar Reads Maximum count of â010..â subsequences that can be removed from given Binary String Given a binary string S consisting of size N, the task is to find the maximum number of binary subsequences of the form "010.." of length at least 2 that can be removed from the given string S. Examples: Input: S = "110011010"Output: 3Explanation:Following are the subsequence removed:Operation 1: Ch 6 min read Frequency of maximum occurring subsequence in given string Given a string str of lowercase English alphabets, our task is to find the frequency of occurrence a subsequence of the string which occurs the maximum times. Examples: Input: s = "aba" Output: 2 Explanation: For "aba", subsequence "ab" occurs maximum times in subsequence 'ab' and 'aba'. Input: s = 6 min read Maximum number of given operations to remove the entire string Given string str containing lowercase English characters, we can perform the following two operations on the given string: Remove the entire string.Remove a prefix of the string str[0...i] only if it is equal to the sub-string str[(i + 1)...(2 * i + 1)]. The task is to find the maximum number of ope 11 min read Minimize removals to remove another string as a subsequence of a given string Given two strings str and X of length N and M respectively, the task is to find the minimum characters required to be removed from the string str such that string str doesn't contain the string X as a subsequence. Examples: Input: str = "btagd", X = "bad"Output: 1Explanation:String "btag" has does n 14 min read Maximum number of set bits count in a K-size substring of a Binary String Given a binary string S of size N and an integer K. The task is to find the maximum number of set bit appears in a substring of size K. Examples: Input: S = "100111010", K = 3 Output: 3 Explanation: The substring "111" contains 3 set bits. Input:S = "0000000", K = 4 Output: 0 Explanation: S doesn't 10 min read Like