File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
Python/chapter01/1.4 - PalinPerm Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ Palindrome Permutation: Given a string, write a function to check if it is a
3
+ permutation of a palin drome. A palindrome is a word or phrase that is the same
4
+ forwards and backwards. A permutation is a rearrangement of letters.
5
+ The palindrome does not need to be limited to just dictionary words.
6
+ EXAMPLE
7
+ Input: Tact Coa
8
+ Output: True (permutations: "taco cat", "atco eta", etc.)
9
+ """
10
+
11
+ def PalinPerm (str ):
12
+ d = {}
13
+ str = str .replace (' ' ,'' ).lower ()#Take out the spaces
14
+ for i in str :
15
+ if i in d :
16
+ d [i ] += 1
17
+ else :
18
+ d [i ] = 1
19
+ odd_count = 0 # pop ppo opp
20
+ for keys ,value in d .items ():
21
+ if value % 2 != 0 and odd_count == 0 :# Remeber there should only be 1 where key equals when val%2
22
+ odd_count += 1
23
+ elif value % 2 != 0 and odd_count != 0 :
24
+ return False
25
+ return True
26
+
27
+ print (PalinPerm ('taco cat' ))#True
28
+ print (PalinPerm ('taco catt' ))#False
You can’t perform that action at this time.
0 commit comments