-
Notifications
You must be signed in to change notification settings - Fork 0
/
157.py
36 lines (26 loc) · 768 Bytes
/
157.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
"""
Problem:
Given a string, determine whether any permutation of it is a palindrome.
For example, carrace should return true, since it can be rearranged to form racecar,
which is a palindrome. daily should return false, since there's no rearrangement that
can form a palindrome.
"""
def is_permutation_palindrome(string: str) -> str:
char_set = set()
for char in string:
if char in char_set:
char_set.remove(char)
else:
char_set.add(char)
length = len(char_set)
if length in (1, 0):
return True
return False
if __name__ == "__main__":
print(is_permutation_palindrome("carrace"))
print(is_permutation_palindrome("daily"))
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(n)
"""