Given a string of length **n** of capital letters. The task is to find the count of ‘GFG’ subsequence in the given string.

**Examples:**

Input : str[] = "GFGFG" Output : 4GFGFG,GFGFG,GFGFG, GFGFGInput : 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:

## C++

`// CPP Program to find the "GFG" subsequence in ` `// the given string ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define MAX 100 ` ` ` `// Print the count of "GFG" subsequence in the string ` `void` `countSubsequence(` `char` `s[], ` `int` `n) ` `{ ` ` ` `int` `cntG = 0, cntF = 0, result = 0, C=0; ` ` ` ` ` `// Traversing the given string ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `switch` `(s[i]) { ` ` ` ` ` `// If the character is 'G', increment ` ` ` `// the count of 'G', increase the result ` ` ` `// and update the array. ` ` ` `case` `'G'` `: ` ` ` `cntG++; ` ` ` `result+=C; ` ` ` `break` `; ` ` ` ` ` `// If the character is 'F', increment ` ` ` `// the count of 'F' and update the array. ` ` ` `case` `'F'` `: ` ` ` `cntF++; ` ` ` `C+=cntG; ` ` ` `break` `; ` ` ` ` ` `// Ignore other character. ` ` ` `default` `: ` ` ` `continue` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `cout << result << endl; ` `} ` ` ` `// Driven Program ` `int` `main() ` `{ ` ` ` `char` `s[] = ` `"GFGFG"` `; ` ` ` `int` `n = ` `strlen` `(s); ` ` ` `countSubsequence(s, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java Program to find the "GFG" subsequence ` `// in the given string ` ` ` `public` `class` `GFG { ` ` ` ` ` `static` `int` `max = ` `100` `; ` ` ` ` ` `// Print the count of "GFG" subsequence ` ` ` `// in the string ` ` ` `static` `void` `countSubsequence(String s, ` `int` `n) ` ` ` `{ ` ` ` `int` `cntG = ` `0` `, cntF = ` `0` `, result = ` `0` `, C=` `0` `; ` ` ` ` ` `// Traversing the given string ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) { ` ` ` `switch` `(s.charAt(i)) { ` ` ` ` ` `// If the character is 'G', ` ` ` `// increment the count of 'G', ` ` ` `// increase the result and ` ` ` `// update the array. ` ` ` `case` `'G'` `: ` ` ` `cntG++; ` ` ` `result+=C; ` ` ` `break` `; ` ` ` ` ` `// If the character is 'F', ` ` ` `// increment the count of 'F' ` ` ` `// and update the array. ` ` ` `case` `'F'` `: ` ` ` `cntF++; ` ` ` `C+=cntG; ` ` ` `break` `; ` ` ` ` ` `// Ignore other character. ` ` ` `default` `: ` ` ` `continue` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `System.out.println(result); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) { ` ` ` `String s= ` `"GFGFG"` `; ` ` ` `int` `n = s.length(); ` ` ` `countSubsequence(s, n); ` ` ` `} ` `} ` ` ` `// This code is contributed by Sam007 ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 Program to find the "GFG" ` `# subsequence in the given string ` `MAX` `=` `100` ` ` `# Print the count of "GFG" subsequence ` `# in the string ` `def` `countSubsequence(s, n): ` ` ` `cntG ` `=` `0` ` ` `cntF ` `=` `0` ` ` `result ` `=` `0` ` ` `C` `=` `0` ` ` ` ` ` ` `# Traversing the given string ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `if` `(s[i] ` `=` `=` `'G'` `): ` ` ` ` ` `# If the character is 'G', increment ` ` ` `# the count of 'G', increase the result ` ` ` `# and update the array. ` ` ` `cntG ` `+` `=` `1` ` ` `result ` `+` `=` `C ` ` ` `continue` ` ` ` ` `# If the character is 'F', increment ` ` ` `# the count of 'F' and update the array. ` ` ` `if` `(s[i] ` `=` `=` `'F'` `): ` ` ` `cntF ` `+` `=` `1` ` ` `C ` `+` `=` `cntG ` ` ` `continue` ` ` ` ` `# Ignore other character. ` ` ` `else` `: ` ` ` `continue` ` ` ` ` `print` `(result) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `s ` `=` `"GFGFG"` ` ` `n ` `=` `len` `(s) ` ` ` `countSubsequence(s, n) ` ` ` `# This code is contributed by ` `# Sanjit_Prasad ` |

*chevron_right*

*filter_none*

## C#

`// C# Program to find the "GFG" subsequence ` `// in the given string ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Print the count of "GFG" subsequence ` ` ` `// in the string ` ` ` `static` `void` `countSubsequence(` `string` `s, ` ` ` `int` `n) ` ` ` `{ ` ` ` `int` `cntG = 0, cntF = 0, result = 0, C=0; ` ` ` ` ` `// Traversing the given string ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `switch` `(s[i]) { ` ` ` ` ` `// If the character is 'G', ` ` ` `// increment the count of 'G', ` ` ` `// increase the result and ` ` ` `// update the array. ` ` ` `case` `'G'` `: ` ` ` `cntG++; ` ` ` `result += C; ` ` ` `break` `; ` ` ` ` ` `// If the character is 'F', ` ` ` `// increment the count of 'F' ` ` ` `// and update the array. ` ` ` `case` `'F'` `: ` ` ` `cntF++; ` ` ` `C+=cntG; ` ` ` `break` `; ` ` ` ` ` `// Ignore other character. ` ` ` `default` `: ` ` ` `continue` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `Console.WriteLine(result); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `string` `s= ` `"GFGFG"` `; ` ` ` `int` `n = s.Length; ` ` ` `countSubsequence(s, n); ` ` ` `} ` `} ` ` ` `// This code is contributed by Sam007. ` |

*chevron_right*

*filter_none*

**Output:**

4

Time Complexity : O(n)

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.

## Recommended Posts:

- Count subsequences in first string which are anagrams of the second string
- Construct a string that has exactly K subsequences from given string
- Check if a string can be obtained by appending subsequences of another string
- Count of possible subarrays and subsequences using given length of Array
- Maximize count of Decreasing Subsequences from the given Array
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9 | Set 2
- Generating distinct subsequences of a given string in lexicographic order
- Lexicographically smallest permutation of a string with given subsequences
- Number of subsequences in a given binary string divisible by 2
- Convert given string to another by minimum replacements of subsequences by its smallest character
- Count all increasing subsequences
- Count minimum number of subsets (or subsequences) with consecutive numbers
- Count all subsequences having product less than K
- Count of subsequences having maximum distinct elements
- 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 the number of contiguous increasing and decreasing subsequences in a sequence
- Count of subsequences in an array with sum less than or equal to X
- Count of subsequences whose product is a difference of square of two integers

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.