Tags: "leetcode", "hashmap", "palindrome", access_time 1-min read

Edit this post on Github

Palindrome Permutation

Created: March 15, 2020 by [lek-tin]

Last updated: March 15, 2020

Given a string, determine if a permutation of the string could form a palindrome.

Example 1

Input: "code"
Output: false

Example 2

Input: "aab"
Output: true

Example 3

Input: "carerac"
Output: true

Solution

class Solution:
    def canPermutePalindrome(self, s: str) -> bool:
        lookup = set()

        for c in s:
            if c not in lookup:
                lookup.add(c)
            else:
                lookup.remove(c)

        return len(lookup) <= 1