The Wayback Machine - https://web.archive.org/web/20240811223030/https://www.geeksforgeeks.org/maximum-number-of-removals-of-given-subsequence-from-a-string/
Open In App

Maximum number of removals of given subsequence from a string

Last Updated : 03 Oct, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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: 1
Explanation: After 1st operation: str = “gsk”
No further operation can be performed.

Input: str = “kgs”
Output: 0

Approach:  

  1. Take three variables g, gk, and gks which will store the occurrence of the sub-sequences ‘g’, ‘gk’, and ‘gks’ respectively.
  2. 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.
  3. 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>


Output

1

Time Complexity: O(n)
Auxiliary Space: O(1)



Similar Reads

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 not contain "bad" as a subsequence. Therefore, only
14 min read
Minimum number of removals required such that no subsequence of length 2 occurs more than once
Given a string S consisting of N lowercase characters, the task is to modify the given string such that no subsequence of length two repeats in the string by removing minimum number of characters. Examples: Input: S = "abcaadbcd"Output: abcdExplanation: Removing the characters at indices {2, 3, 4, 5, 6, 7} modifies the string to "abcd", that contai
7 min read
Maximum inversions possible after exactly K removals from given array
Given an array arr[] consisting of N integers, and an integer K, the task is to find the maximum number of inversions of the given array possible after the removal of K array elements. Examples: Input: arr[] = {2, 3, 4, 1}, K = 2Output: 1Explanation: Removing the 1st and 2nd array elements modifies the array to {4, 1}. Therefore, the maximum number
10 min read
Minimum removals required such that given string consists only of a pair of alternating characters
Given a string S, the task is to find the minimum removal of characters required such that the string S consists only of two alternating characters. Examples: Input: S = "adebbeeaebd"Output: 7Explanation: Removing all occurrences of 'b' and 'e' modifies the string to "adad", which consist of alternating occurrences of 'a' and 'd'. Input: S = "abccd
10 min read
Count maximum occurrence of subsequence in string such that indices in subsequence is in A.P.
Given a string S, the task is to count the maximum occurrence of subsequences in the given string such that the indices of the characters of the subsequence are Arithmetic Progression. Examples: Input: S = "xxxyy" Output: 6 Explanation: There is a subsequence "xy", where indices of each character of the subsequence are in A.P. The indices of the di
12 min read
Minimize difference between maximum and minimum array elements by exactly K removals
Given an array arr[] consisting of N positive integers and an integer K, the task is to minimize the difference between the maximum and minimum element in the given array after removing exactly K elements. Examples: Input: arr[] = {5, 1, 6, 7, 12, 10}, K = 3Output: 2Explanation:Remove elements 12, 10 and 1 from the given array.The array modifies to
6 min read
Maximum removals possible from an array such that sum of its elements is greater than or equal to that of another array
Given two arrays arr[] and brr[] of size N and M respectively, the task is to count the maximum number of elements that can be removed from the array arr[] such that the sum of elements in arr[] is greater than or equal to the sum of elements in brr[]. Examples: Input: arr[] = { 1, 2, 4, 6 }, brr[] = { 7 } Output: 2 Explanation: Removing arr[2] mod
8 min read
Minimum removals required to place all 0s before 1s in a Binary String
Given a binary string S, the task is to find the minimum number of characters required to be removed from S, such that all the 0s are placed before 1s. Examples: Input: S = "001101"Output: 1Explanation: Removing S[4] (= '0') modifies the string S to "00111".Therefore, minimum number of deletions required is 1. Input: S = 01001Output: 1Explanation:
6 min read
Minimum substring removals required to make all remaining characters of a string same
Given a string str of length N, the task is to find the minimum number of substrings required to be removed to make all the remaining characters of the string same. Note: The substring to be removed must not contain the last remaining character in the string. Examples: Input: str = "ACBDAB" Output: 2 Explanation: Removing the substring { str[1], ..
7 min read
Count minimum substring removals required to reduce string to a single distinct character
Given a string S consisting of 'X', 'Y' and 'Z' only, the task is to convert S to a string consisting of only a single distinct character by selecting a character and removing substrings that does not contain that character, minimum number of times. Note: Once a character is chosen, no other character can be used in further operations. Examples: In
7 min read
Practice Tags :
three90RightbarBannerImg