Skip to content

Commit

Permalink
feature(C01_P04): Add XOR implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
LibenHailu authored and brycedrennan committed Dec 18, 2022
1 parent 6994ad0 commit 6be0972
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions chapter_01/p04_palindrome_permutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ def is_palindrome_bit_vector(phrase):
return (r - 1) & r == 0


def is_palindrome_bit_vector2(phrase):
"""checks if a string is a permutation of a palindrome using XOR operation"""
count_odd = 0
for c in phrase:
val = char_number(c)
if val == -1:
continue
count_odd ^= 1 << val

return count_odd & count_odd - 1 == 0


def is_palindrome_permutation_pythonic(phrase):
"""function checks if a string is a permutation of a palindrome or not"""
counter = Counter(clean_phrase(phrase))
Expand Down Expand Up @@ -79,6 +91,7 @@ class Test(unittest.TestCase):
is_palindrome_permutation,
is_palindrome_bit_vector,
is_palindrome_permutation_pythonic,
is_palindrome_bit_vector2,
]

def test_pal_perm(self):
Expand Down

0 comments on commit 6be0972

Please sign in to comment.