Given a string of length n of capital letters. The task is to find the count of ‘GFG’ subsequence in the given string.
Input : str = "GFGFG" Output : 4 GFGFG, GFGFG, GFGFG, GFGFG Input : str = "ABCFGFPG" Output : 1
To find the number of “GFG” subsequences in the given string, observe for each ‘F’ if we know number of ‘G’ before and after it. Then the number of “GFG” subsequence for that ‘F’ is equal to product of number of ‘G’ before and after that ‘F’.
So, the idea is to maintain an array, arr, where arr[i] store number of ‘G’ before index i, if ith character of the string is ‘F’ and number of ‘F’ before index i, if the ith character is ‘G’.
Also, we will calculate and store the number of “GFG” subsequence in result whenever we encounter ‘G’.
Below is the implementation of this approach:
Time Complexity : O(n)
- Count subsequences in first string which are anagrams of the second string
- Count Distinct Subsequences
- Count all increasing subsequences
- Count all subsequences having product less than K
- Find the count of subsequences where each element is divisible by K
- Count the number of subsequences of length k having equal LCM and HCF
- Count of subsequences having maximum distinct elements
- Print all subsequences of a string
- Count minimum number of subsets (or subsequences) with consecutive numbers
- Print all subsequences of a string using ArrayList
- Number of subsequences in a string divisible by n
- Print all subsequences of a string | Iterative Method
- Lexicographically smallest permutation of a string with given subsequences
- Number of subsequences as "ab" in a string repeated K times
- Number of subsequences in a given binary string divisible by 2
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.