Find the count of sub | Function to return the count of required occurrence ; To store the count of occurrences ; Check first four characters from ith position ; Variables for counting the required characters ; Check the four contiguous characters which can be reordered to form ' clap ' ; If all four contiguous characters are present then increment cnt variable ; Driver code
function countOcc ( s ) { var cnt = 0 ; for ( var i = 0 ; i < s . length - 3 ; i ++ ) { var c = 0 , l = 0 , a = 0 , p = 0 ; for ( var j = i ; j < i + 4 ; j ++ ) { switch ( s [ j ] ) { case ' ' : c ++ ; break ; case ' ' : l ++ ; break ; case ' ' : a ++ ; break ; case ' ' : p ++ ; break ; } } if ( c == 1 && l == 1 && a == 1 && p == 1 ) cnt ++ ; } return cnt ; } var s = " " ; s = s . toLowerCase ( ) ; document . write ( countOcc ( s ) ) ;