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.
Input: str = "ggkssk" Output: 1 After 1st operation: str = "gsk" No further operation can be performed. Input: str = "kgs" Output: 0
- 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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find number of times a string occurs as a subsequence in given string
- Frequency of maximum occurring subsequence in given string
- Longest subsequence such that every element in the subsequence is formed by multiplying previous element with a prime
- Find the equal pairs of subsequence of S and subsequence of T
- Maximum length Subsequence with alternating sign and maximum Sum
- Maximum length prefix of one string that occurs as subsequence in another
- Find the Maximum Alternate Subsequence Sum from a given array
- Find maximum Subsequence Sum according to given conditions
- Maximum subsequence sum from a given array which is a perfect square
- Given two strings, find if first string is a subsequence of second
- Count All Palindromic Subsequence in a given String
- Count subsequence of length three in a given string
- Minimize elements to be added to a given array such that it contains another given array as its subsequence
- Given a large number, check if a subsequence of digits is divisible by 8
- Longest subsequence whose sum is divisible by a given number
- Find the Increasing subsequence of length three with maximum product
- Printing Maximum Sum Increasing Subsequence
- Subsequence with maximum odd sum
- Maximum product of subsequence of size k
- Maximum length subsequence possible of the form R^N K^N
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.