JavaScript 84ms solution, very simple w/ comments


  • 1
    K
    var canPermutePalindrome = function(s) {
        var found = {} // keep track of odd occurrences
        for (var i = 0; i < s.length; i++) {
            // we have already seen this one, so remove it
            if (s[i] in found) {
                delete found[s[i]] 
            }   
            // we have not seen this one, so add it
            else {
                found[s[i]] = true
            }
        }
        // we should only have, at most, one key left that was not removed (one odd number of occurrences)
        return Object.keys(found).length <= 1
    }

Log in to reply
 

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