Maximum number of removals of given subsequence from a string | Function to return max possible operation of the given type that can be performed on str ; Increment count of sub - sequence ' g ' ; Increment count of sub - sequence ' gk ' if ' g ' is available ; Increment count of sub - sequence ' gks ' if sub - sequence ' gk ' appeared previously ; Return the count of sub - sequence ' gks ' ; Driver code
< ? php function maxOperations ( $ str ) { $ i = $ g = $ gk = $ gks = 0 ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) { if ( $ str [ $ i ] == ' g ' ) { $ g ++ ; } else if ( $ str [ $ i ] == ' k ' ) { if ( $ g > 0 ) { $ g -- ; $ gk ++ ; } } else if ( $ str [ $ i ] == ' s ' ) { if ( $ gk > 0 ) { $ gk -- ; $ gks ++ ; } } } return $ gks ; } $ a = " ggkssk " ; echo maxOperations ( $ a ) ; ? >