Count substrings with each character occurring at most k times | function to find number of substring in which each character has count less than or equal to k . ; initialize left and right pointer to 0 ; an array to keep track of count of each alphabet ; decrement the count ; increment left pointer ; Driver code
function find_sub ( s , k ) { let len = s . length ; let lp = 0 , rp = 0 ; let ans = 0 ; let hash_char = new Array ( 26 ) ; for ( let i = 0 ; i < hash_char . length ; i ++ ) { hash_char [ i ] = 0 ; } for ( ; rp < len ; rp ++ ) { hash_char [ s [ rp ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; while ( hash_char [ s [ rp ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] > k ) { hash_char [ s [ lp ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] -- ; lp ++ ; } ans += rp - lp + 1 ; } return ans ; } let S = " " ; let k = 2 ; document . write ( find_sub ( S , k ) ) ;