-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathis_palindrome_permutation.py
44 lines (39 loc) · 1.62 KB
/
is_palindrome_permutation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
def is_palindrome_permutation(value):
"""
195. Palindrome Permutation: Given a string, write a function to check if it is a permutation of a palindrome.
"""
length = len(value)
records = []
for i in range(ord('a'), ord('z') + 1):
records.append(0)
# Flip and unflip the values from 0-->1 & 1-->0
for i in value:
pos = ord(i) - 97
if records[pos] == 1:
records[pos] = 0
elif records[pos] == 0:
records[pos] = 1
if len(value) % 2 == 0:
return sum(records) == 0
else:
return sum(records) == 1
if __name__ == "__main__":
assert is_palindrome_permutation('level') is True
assert is_palindrome_permutation('tacocat') is True
assert is_palindrome_permutation('poop') is True
assert is_palindrome_permutation('poop') is True
assert is_palindrome_permutation('nitin') is True
assert is_palindrome_permutation('level') is True
assert is_palindrome_permutation('level') is True
assert is_palindrome_permutation('level') is True
assert is_palindrome_permutation('madam') is True
assert is_palindrome_permutation('mom') is True
assert is_palindrome_permutation('racecar') is True
assert is_palindrome_permutation('redder') is True
assert is_palindrome_permutation('repaper') is True
assert is_palindrome_permutation('rotator') is True
assert is_palindrome_permutation('prashant') is False
assert is_palindrome_permutation('loop') is False
assert is_palindrome_permutation('') is True
assert is_palindrome_permutation('s') is True
assert is_palindrome_permutation('xxx') is True