Solid JavaScript solution

  • 1

    All we have to do is keep track of all the characters in the string that don't have a "partner" that it can mirror.

    A palindrome with an odd length will only have one non-mirrored character. A palindrome with an even length will have zero non-mirrored characters. Therefore, there should never be more than one non-mirrored character.

    var canPermutePalindrome = function(s) {
        let unique = new Set();
        for (let i = 0; i < s.length; i++) {
            let ch = s.charAt(i);
            if (unique.has(ch)) unique.delete(ch);
            else unique.add(ch);
        return unique.size <= 1;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.