Check if permutation of a given string can be made palindromic by removing at most K characters | Function to check if the satisfies the given conditions or not ; Stores length of given string ; Stores frequency of each character of str ; Update frequency of current character ; Stores count of distinct character whose frequency is odd ; Traverse the cntFreq [ ] array . ; If frequency of character i is odd ; Update cntOddFreq ; If count of distinct character having odd frequency is <= K + 1 ; Driver Code ; If str satisfy the given conditions
def checkPalinK ( str , K ) : NEW_LINE INDENT N = len ( str ) NEW_LINE cntFreq = [ 0 ] * 256 NEW_LINE for i in range ( N ) : NEW_LINE INDENT cntFreq [ ord ( str [ i ] ) ] += 1 NEW_LINE DEDENT cntOddFreq = 0 NEW_LINE for i in range ( 256 ) : NEW_LINE INDENT if ( cntFreq [ i ] % 2 == 1 ) : NEW_LINE INDENT cntOddFreq += 1 NEW_LINE DEDENT DEDENT if ( cntOddFreq <= ( K + 1 ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " geeksforgeeks " NEW_LINE K = 2 NEW_LINE if ( checkPalinK ( str , K ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT