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 poof 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
freq = [ 0 ] * 26 NEW_LINE def checkPalindrome ( ) : NEW_LINE INDENT oddCnt = 0 NEW_LINE for x in freq : NEW_LINE INDENT if ( x % 2 == 1 ) : NEW_LINE INDENT oddCnt += 1 NEW_LINE DEDENT DEDENT return oddCnt <= 1 NEW_LINE DEDENT def countPalindromePermutation ( s , k ) : NEW_LINE INDENT for i in range ( k ) : NEW_LINE INDENT freq [ ord ( s [ i ] ) - 97 ] += 1 NEW_LINE DEDENT ans = 0 NEW_LINE if ( checkPalindrome ( ) ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT i = 0 NEW_LINE j = k NEW_LINE while ( j < len ( s ) ) : NEW_LINE INDENT freq [ ord ( s [ i ] ) - 97 ] -= 1 NEW_LINE i += 1 NEW_LINE freq [ ord ( s [ j ] ) - 97 ] += 1 NEW_LINE j += 1 NEW_LINE if ( checkPalindrome ( ) ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT str = " abbaca " NEW_LINE K = 3 NEW_LINE print ( countPalindromePermutation ( str , K ) ) NEW_LINE