11
11
from typing import Callable
12
12
13
13
14
- def is_permutation_of_palindrome (word : str ) -> bool :
14
+ def is_permutation_of_palindrome (s : str ) -> bool :
15
15
"""
16
- Checks if a word is a palindrome by analyzing the frequencies of characters.
16
+ Checks if a string is a palindrome by analyzing the frequencies of characters.
17
17
A palindrome is a word or phrase that is the same forwards and backwards.
18
18
When determining character frequencies, word will be put to lowercase
19
19
and spaces will not be counted
20
20
A palindrome also has the following property:
21
- * all characters have an even count or all except one are even
21
+ * at most one character appears an odd number of times
22
22
Runtime: O(n)
23
23
Space Complexity: O(n)
24
- :param word : the word we check, 'word ' is a possible permutation of a palindrome
25
- :return: true if word is a permutation of a palindrome, false otherwise
24
+ :param s : the string we check, 's ' is a possible permutation of a palindrome
25
+ :return: true if s is a permutation of a palindrome, false otherwise
26
26
"""
27
- char_frequencies = collections .Counter (word .lower ().replace (' ' , '' ))
27
+ char_frequencies = collections .Counter (s .lower ().replace (' ' , '' ))
28
28
num_odd_freq_chars = 0
29
29
30
30
for key , val in char_frequencies .items ():
@@ -35,21 +35,21 @@ def is_permutation_of_palindrome(word: str) -> bool:
35
35
return True
36
36
37
37
38
- def _is_palindrome (word : str ) -> bool :
38
+ def _is_palindrome (s : str ) -> bool :
39
39
"""
40
- Checks if word is a palindrome by checking if the forward version is the same as the backward version.
40
+ Checks if s is a palindrome by checking if the forward version is the same as the backward version.
41
41
A palindrome is a word or phrase that is the same forwards and backwards.
42
42
Whitespace will not be considered when determining palindrome.
43
43
This function is case insensitive.
44
- :param word : the word we check, possible permutation of a palindrome
45
- :return: true if word is a palindrome, false otherwise
44
+ :param s : the string we check, possible permutation of a palindrome
45
+ :return: true if s is a palindrome, false otherwise
46
46
"""
47
- word_no_spaces = word .replace (' ' , '' ).lower ()
48
- reversed_word = word_no_spaces [::- 1 ]
49
- return word_no_spaces == reversed_word
47
+ s_no_spaces = s .replace (' ' , '' ).lower ()
48
+ reversed_s = s_no_spaces [::- 1 ]
49
+ return s_no_spaces == reversed_s
50
50
51
51
52
- def is_permutation_of_palindrome_brute_force (word : str ) -> bool :
52
+ def is_permutation_of_palindrome_brute_force (s : str ) -> bool :
53
53
"""
54
54
Given a string, this function will return whether the string is a permutation of a palindrome.
55
55
A palindrome is a word or phrase that is the same forwards and backwards.
@@ -62,12 +62,11 @@ def is_permutation_of_palindrome_brute_force(word: str) -> bool:
62
62
Space complexity is O(n!)
63
63
Given: Expect:
64
64
Tact Coa True (permutations: "taco cat", "atco cta")
65
-
66
- :param word: the string that we want to check for perm of a palindrome
67
- :return: True if word is a palindrome, False otherwise.
65
+ :param s: the string that we want to check for perm of a palindrome
66
+ :return: True if s is a palindrome, False otherwise.
68
67
"""
69
- word_no_spaces = word .replace (' ' , '' )
70
- perms = ['' .join (p ) for p in it .permutations (word_no_spaces )]
68
+ s_no_spaces = s .replace (' ' , '' )
69
+ perms = ['' .join (p ) for p in it .permutations (s_no_spaces )]
71
70
for p in perms :
72
71
if _is_palindrome (p ):
73
72
return True
@@ -86,8 +85,8 @@ def _run_tests(self, f: Callable[[str], bool]) -> None:
86
85
("" , True ),
87
86
("a" , True )
88
87
]
89
- for word , expected in cases :
90
- self .assertEqual (f (word ), expected , msg = word )
88
+ for s , expected in cases :
89
+ self .assertEqual (f (s ), expected , msg = s )
91
90
92
91
def test_is_permutation_of_palindrome (self ):
93
92
self ._run_tests (is_permutation_of_palindrome_brute_force )
@@ -106,8 +105,8 @@ def test_is_palindrome(self):
106
105
("Tacoo Cat" , True ),
107
106
("Tacooo Cat" , True )
108
107
]
109
- for word , expected in cases :
110
- self .assertEqual (_is_palindrome (word ), expected , msg = word )
108
+ for s , expected in cases :
109
+ self .assertEqual (_is_palindrome (s ), expected , msg = s )
111
110
112
111
113
112
if __name__ == '__main__' :
0 commit comments