Count of non | Function to return the count of required non - overlapping sub - strings ; To store the required count ; If "010" matches the sub - string starting at current index i ; If "101" matches the sub - string starting at current index i ; Driver code
function countSubStr ( s , n ) { var count = 0 ; for ( i = 0 ; i < n - 2 ; ) { if ( s [ i ] == ' ' && s [ i + 1 ] == ' ' && s [ i + 2 ] == ' ' ) { count ++ ; i += 3 ; } else if ( s [ i ] == ' ' && s [ i + 1 ] == ' ' && s [ i + 2 ] == ' ' ) { count ++ ; i += 3 ; } else { i ++ ; } } return count ; } var s = " " ; var n = s . length ; document . write ( countSubStr ( s , n ) ) ;