Number of subsequences as " ab " in a string repeated K times | PHP code to find number of subsequences of " ab " in the string S which is repeated K times . ; Count of ' a ' s ; Count of ' b ' s ; occurrence of " ab " s in string S ; Add following two : 1 ) K * ( Occurrences of " ab " in single string ) 2 ) a is from one string and b is from other . ; Driver code
< ? php function countOccurrences ( $ s , $ K ) { $ n = strlen ( $ s ) ; $ C = 0 ; $ c1 = 0 ; $ c2 = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ s [ $ i ] == ' a ' ) $ c1 ++ ; if ( $ s [ $ i ] == ' b ' ) { $ c2 ++ ; $ C = $ C + $ c1 ; } } return $ C * $ K + ( $ K * ( $ K - 1 ) / 2 ) * $ c1 * $ c2 ; } $ S = " abcb " ; $ k = 2 ; echo countOccurrences ( $ S , $ k ) , " STRNEWLINE " ; ? >