Count of K | To store the frequency array ; Function to check palindromic of of any substring using frequency array ; Initialise the odd count ; Traversing frequency array to compute the count of characters having odd frequency ; Returns true if odd count is atmost 1 ; Function to count the total number substring whose any permutations are palindromic ; Computing the frequency of first K character of the string ; To store the count of palindromic permutations ; Checking for the current window if it has any palindromic permutation ; Start and end point of window ; Decrementing count of first element of the window ; Incrementing count of next element of the window ; Checking current window character frequency count ; Return the final count ; Given string str ; Window of size K ; Function Call
var freq = Array ( 26 ) . fill ( 0 ) ; function checkPalindrome ( ) { var oddCnt = 0 ; freq . forEach ( x => { if ( x % 2 == 1 ) oddCnt ++ ; } ) ; return oddCnt <= 1 ; } function countPalindromePermutation ( s , k ) { for ( var i = 0 ; i < k ; i ++ ) { freq [ s [ i ] . charCodeAt ( 0 ) - 97 ] ++ ; } var ans = 0 ; if ( checkPalindrome ( ) ) { ans ++ ; } var i = 0 , j = k ; while ( j < s . length ) { freq [ s [ i ++ ] . charCodeAt ( 0 ) - 97 ] -- ; freq [ s [ j ++ ] . charCodeAt ( 0 ) - 97 ] ++ ; if ( checkPalindrome ( ) ) { ans ++ ; } } return ans ; } var str = " " ; var K = 3 ; document . write ( countPalindromePermutation ( str , K ) ) ;