Partition a string into palindromic strings of at least length 2 with every character present in a single string | Function to check if a string can be split into palindromic strings of at least length 2 by including every character exactly once ; Stores the frequency of each character ; Store the frequency of characters with frequencies 1 and even respectively ; Traverse the string s ; Iterate over all the characters ; If the frequency is 1 ; If frequency is even ; Print the result ; Stores the number of characters with frequency equal to 1 that are not part of a palindromic string ; Iterate over all the characters ; If o becomes less than 0 , then break out of the loop ; If frequency of the current character is > 2 and is odd ; Update the value of o ; If a single character is still remaining ; Increment o by 1 ; Set a [ i ] to 1 ; Print the result ; Driver Code
def checkPalindrome ( s ) : NEW_LINE INDENT a = [ 0 ] * 26 NEW_LINE o , e = 0 , 0 NEW_LINE for i in s : NEW_LINE INDENT a [ ord ( i ) - 97 ] += 1 NEW_LINE DEDENT for i in range ( 26 ) : NEW_LINE INDENT if ( a [ i ] == 1 ) : NEW_LINE INDENT o += 1 NEW_LINE DEDENT elif ( a [ i ] % 2 == 0 and a [ i ] != 0 ) : NEW_LINE INDENT e += ( a [ i ] // 2 ) NEW_LINE DEDENT DEDENT if ( e >= o ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT o = o - e NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if ( o <= 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( o > 0 and a [ i ] % 2 == 1 and a [ i ] > 2 ) : NEW_LINE INDENT k = o NEW_LINE o = o - a [ i ] // 2 NEW_LINE if ( o > 0 or 2 * k + 1 == a [ i ] ) : NEW_LINE INDENT o += 1 NEW_LINE a [ i ] = 1 NEW_LINE DEDENT DEDENT DEDENT if ( o <= 0 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " abbbaddzcz " NEW_LINE checkPalindrome ( S ) NEW_LINE DEDENT