forked from vprusso/youtube_tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalindrome_permutation.py
36 lines (29 loc) · 1.1 KB
/
palindrome_permutation.py
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
# YouTube Video: https://www.youtube.com/watch?v=71UjBGz-o0w
"""
1.4
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.
"""
palindrome = "A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal-Panama!"
not_palindrome = "LucidProgramming"
def is_palindrome_perm(input_str):
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
input_str = input_str.replace(" ", "")
input_str = input_str.lower()
hist = dict.fromkeys(list(alphabet.lower()), 0)
for letter in input_str:
if letter in alphabet.lower():
hist[letter] += 1
strike = 0
odd_strike = 0
for letter in hist:
if hist[letter] % 2 != 0 and hist[letter] > 1:
odd_strike += 1
if hist[letter] == 1:
strike += 1
if strike == 2 or odd_strike == 2:
return False
return True
print(is_palindrome_perm("XXXYYY"))