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 all subsequences having product less than K
- Count Distinct Subsequences
- Count all increasing subsequences
- 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
- Number of subsequences as "ab" in a string repeated K times
- Lexicographically smallest permutation of a string with given subsequences
- Print all Subsequences of String which Start with Vowel and End with Consonant.
- Number of ways to partition a string into two balanced subsequences
- Generating distinct subsequences of a given string in lexicographic order
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.