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