Count of sub | Function to return the number of sub - strings that do not contain the given character c ; Length of the string ; Traverse in the string ; If current character is different from the given character ; Update the number of sub - strings ; Reset count to 0 ; For the characters appearing after the last occurrence of c ; Driver code
< ? php function countSubstrings ( $ s , $ c ) { $ n = strlen ( $ s ) ; $ cnt = 0 ; $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ s [ $ i ] != $ c ) $ cnt ++ ; else { $ sum += floor ( ( $ cnt * ( $ cnt + 1 ) ) / 2 ) ; $ cnt = 0 ; } } $ sum += floor ( ( $ cnt * ( $ cnt + 1 ) ) / 2 ) ; return $ sum ; } $ s = " baa " ; $ c = ' b ' ; echo countSubstrings ( $ s , $ c ) ; ? >