Check if a string can be made empty by repeatedly removing given subsequence | Java program for the above approach ; Function to check if a string can be made empty by removing all subsequences of the form " GFG " or not ; Driver Code
import java . io . * ; class GFG { public static void findIfPossible ( int N , String str ) { int countG = 0 , countF = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( str . charAt ( i ) == ' G ' ) countG ++ ; else countF ++ ; } if ( 2 * countF != countG ) { System . out . println ( " No " ) ; } else { int id = 0 ; boolean flag = true ; for ( int i = 0 ; i < N ; i ++ ) { if ( str . charAt ( i ) == ' G ' ) { countG -- ; id ++ ; } else { countF -- ; id -- ; } if ( id < 0 ) { flag = false ; break ; } if ( countG < countF ) { flag = false ; break ; } } if ( flag ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } } public static void main ( String [ ] args ) { int n = 6 ; String str = " GFGFGG " ; findIfPossible ( n , str ) ; } }