File tree Expand file tree Collapse file tree 1 file changed +5
-4
lines changed
Python/chapter01/1.4 - PalinPerm Expand file tree Collapse file tree 1 file changed +5
-4
lines changed Original file line number Diff line number Diff line change @@ -42,6 +42,8 @@ def _is_palindrome(s: str) -> bool:
42
42
A palindrome is a word or phrase that is the same forwards and backwards.
43
43
Whitespace will not be considered when determining palindrome.
44
44
This function is case insensitive.
45
+ Runtime: O(n)
46
+ Space Complexity: O(n)
45
47
:param s: the string we check, possible permutation of a palindrome
46
48
:return: true if s is a palindrome, false otherwise
47
49
"""
@@ -60,16 +62,15 @@ def is_permutation_of_palindrome_brute_force(s: str) -> bool:
60
62
the reversed version. So, the space will not be taken into account only when determining palindrome.
61
63
Assuming ASCII
62
64
Runtime is O(n!)
63
- Space complexity is O(n! )
65
+ Space complexity is O(n)
64
66
Given: Expect:
65
67
Tact Coa True (permutations: "taco cat", "atco cta")
66
68
:param s: the string that we want to check for perm of a palindrome
67
69
:return: True if s is a palindrome, False otherwise.
68
70
"""
69
71
s_no_spaces = s .replace (' ' , '' )
70
- perms = ['' .join (p ) for p in it .permutations (s_no_spaces )]
71
- for p in perms :
72
- if _is_palindrome (p ):
72
+ for p in it .permutations (s_no_spaces ):
73
+ if _is_palindrome ('' .join (p )):
73
74
return True
74
75
return False
75
76
You can’t perform that action at this time.
0 commit comments