-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindrome_Permutation.py
More file actions
67 lines (53 loc) · 1.61 KB
/
Palindrome_Permutation.py
File metadata and controls
67 lines (53 loc) · 1.61 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'''
Palindrome Permutation: Given a string, write a function to check if it is a permutation of a palindrome. A palindrome is a word or phrase that is the same forwards and backwards. A permutation
is a rearrangement of letters. The palindrome does not need to be limited to just dictionary words.
EXAMPLE
Input: Tact Coa
Output: True (permutations : "taco cat" , "atco cta" , etc. )
Hints: #106, h 0134, § 136
'''
#Solution 1
from operator import countOf
from collections import Counter
def Even_palindrome_check(str):
count = [0] * 256
for i in range(len(str)):
if(count[ord(str[i])] == 0):
count[ord(str[i])] += 1
else:
count[ord(str[i])] -= 1
for i in range(256):
if (count[i] != 0):
return False
return True
def Odd_palindrome_check(str):
count = [0] * 256
for i in range(len(str)):
if(count[ord(str[i])] == 0):
count[ord(str[i])] += 1
else:
count[ord(str[i])] -= 1
if (count.count(1) == 1):
return True
else:
return False
def Palindrome_Permutation_1(str1):
str1 = "".join(tuple(str1.strip().lower().split(" ")))
if (len(str1) % 2 == 0):
return Even_palindrome_check(str1)
else:
return Odd_palindrome_check(str1)
#Solution 2
def Pal_Per(s):
cnt = Counter(s)
odd = 0
for freq in cnt.values():
if freq % 2 != 0:
odd = odd + 1
if odd > 1:
return False
return True
if __name__ == '__main__':
str = input("Enter String >> ")
answer = Pal_Per(str)
print(answer)