Skip to content

Commit 945e51c

Browse files
Palindrome_Permutation Done
1 parent 496ab92 commit 945e51c

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'''
2+
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
3+
is a rearrangement of letters. The palindrome does not need to be limited to just dictionary words.
4+
EXAMPLE
5+
Input: Tact Coa
6+
Output: True (permutations : "taco cat" , "atco cta" , etc. )
7+
Hints: #106, h 0134, § 136
8+
'''
9+
10+
from operator import countOf
11+
12+
13+
def Even_palindrome_check(str):
14+
count = [0] * 256
15+
16+
for i in range(len(str)):
17+
if(count[ord(str[i])] == 0):
18+
count[ord(str[i])] += 1
19+
else:
20+
count[ord(str[i])] -= 1
21+
22+
for i in range(256):
23+
if (count[i] != 0):
24+
return False
25+
26+
return True
27+
28+
def Odd_palindrome_check(str):
29+
count = [0] * 256
30+
31+
for i in range(len(str)):
32+
if(count[ord(str[i])] == 0):
33+
count[ord(str[i])] += 1
34+
else:
35+
count[ord(str[i])] -= 1
36+
37+
if (count.count(1) == 1):
38+
return True
39+
else:
40+
return False
41+
42+
def Palindrome_Permutation(str1):
43+
str1 = "".join(tuple(str1.strip().lower().split(" ")))
44+
if (len(str1) % 2 == 0):
45+
return Even_palindrome_check(str1)
46+
else:
47+
return Odd_palindrome_check(str1)
48+
49+
50+
if __name__ == '__main__':
51+
str = input("Enter String >> ")
52+
answer = Palindrome_Permutation(str)
53+
print(answer)

0 commit comments

Comments
 (0)