Skip to main content
Tweeted twitter.com/StackCodeReview/status/789879758595223552
deleted 1 character in body; edited tags
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

I see a couple of these on codereview but I was hoping my way hasn't yet been encountered:

def is_palindrome_permutation(strng):
    cache = set()
    for char in strng.lower():
        if not char.isalpha():
            continue
        if char in cache:
            cache.remove(char)
        else:
            cache.add(char)

    return len(cache) <= 1

Instead of maintaining any counts of characters to check for oddness or evenness, simply add and remove characters from a set. A character repeated an even number of times will be removed from the set; a character repeated an odd number of times will remain in the set. If the final set has a length less than or equal to one, it is a permutation of a palindrome.

I see a couple of these on codereview but I was hoping my way hasn't yet been encountered:

def is_palindrome_permutation(strng):
    cache = set()
    for char in strng.lower():
        if not char.isalpha():
            continue
        if char in cache:
            cache.remove(char)
        else:
            cache.add(char)

    return len(cache) <= 1

Instead of maintaining any counts of characters to check for oddness or evenness, simply add and remove characters from a set. A character repeated an even number of times will be removed from the set; a character repeated an odd number of times will remain in the set. If the final set has a length less than or equal to one, it is a permutation of a palindrome.

I see a couple of these on codereview but I was hoping my way hasn't yet been encountered:

def is_palindrome_permutation(strng):
    cache = set()
    for char in strng.lower():
        if not char.isalpha():
            continue
        if char in cache:
            cache.remove(char)
        else:
            cache.add(char)

    return len(cache) <= 1

Instead of maintaining any counts of characters to check for oddness or evenness, simply add and remove characters from a set. A character repeated an even number of times will be removed from the set; a character repeated an odd number of times will remain in the set. If the final set has a length less than or equal to one, it is a permutation of a palindrome.

Source Link

Check if a string is a permutation of a palindrome

I see a couple of these on codereview but I was hoping my way hasn't yet been encountered:

def is_palindrome_permutation(strng):
    cache = set()
    for char in strng.lower():
        if not char.isalpha():
            continue
        if char in cache:
            cache.remove(char)
        else:
            cache.add(char)

    return len(cache) <= 1

Instead of maintaining any counts of characters to check for oddness or evenness, simply add and remove characters from a set. A character repeated an even number of times will be removed from the set; a character repeated an odd number of times will remain in the set. If the final set has a length less than or equal to one, it is a permutation of a palindrome.