-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path157_Amazon_Find_If_String_Can_Be _Made_Palindrome.py
More file actions
executable file
·49 lines (30 loc) · 1.27 KB
/
157_Amazon_Find_If_String_Can_Be _Made_Palindrome.py
File metadata and controls
executable file
·49 lines (30 loc) · 1.27 KB
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
"""
This problem was asked by Amazon.
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 permutes_to_palindrome(string):
def char_to_frequency():
freq_map = {}
for char in string:
if char not in freq_map:
freq_map[char] = 0
freq_map[char] += 1
return freq_map
def freq_in_multiples_of_2(freq_map):
count = 0
# at most only one char cannot have frequency in multiples of two
for char, freq in frequency_map.items():
if freq % 2 != 0:
count += 1
if count > 1:
return False
return True
frequency_map = char_to_frequency()
return freq_in_multiples_of_2(frequency_map)
if __name__ == '__main__':
print(permutes_to_palindrome("carrace")) # True, c:2, a:2, r:2, e:1
print(permutes_to_palindrome("daily")) # False d:1, a:1, i:1, l:1, y:1
print(permutes_to_palindrome("jjjjje")) # False j:5, e:5
print(permutes_to_palindrome("jjjjjeecc")) # True j:5, e:2, c:2