Palindromic Permutation

This post explores the question of if the characters of a given word can rearranged such that the final word is a palindrome. For example, the word "edified" could be permuted to form "deified".

Approach

The key idea to recognize in this problem is understanding that there the same count of the same characters on both sides of the mid-point. In other word, as long as the count of characters zero each other out such that there are zero or one character left, then the given word is palindromic permutable.

Implementation

public static boolean canFormPalindrome(String word) {
    Set<Character> oddFrequencyCharacters = new HashSet<>();
    for (int i = 0; i < word.length(); i++) {
        Character c = word.charAt(i);
        if (!oddFrequencyCharacters.contains(c))
            oddFrequencyCharacters.add(c);
        else
            oddFrequencyCharacters.remove(c);
    }

    return oddFrequencyCharacters.size() <= 1;
}

This article is my 31st oldest. It is 127 words long, and it’s got 0 comments for now.