-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutation_palindrome.py
More file actions
48 lines (35 loc) · 981 Bytes
/
permutation_palindrome.py
File metadata and controls
48 lines (35 loc) · 981 Bytes
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
45
46
47
48
"""
https://www.interviewcake.com/question/python/permutation-palindrome
"""
def palindrome(string):
counter = {}
len_string = 0
for i in string:
len_string += 1
if counter.get(i):
counter[i] += 1
else:
counter[i] = 1
num_odds_allowed = 1 if len_string % 2 != 0 else 0
for value in counter.values():
if value % 2 != 0:
if num_odds_allowed:
num_odds_allowed -= 1
else:
return False
return True
def palindrome_set(string):
counter = set()
len_string = 0
for i in string:
if i in counter:
counter.remove(i)
else:
counter.add(i)
len_string += 1
num_odds_allowed = 1 if len_string % 2 != 0 else 0
return len(counter) == num_odds_allowed
assert palindrome_set('civic')
assert palindrome_set('ivicc')
assert not palindrome_set('civil')
assert not palindrome_set('livci')