Skip to content

Commit 595f619

Browse files
committed
update runtime/space compexity, and O(n!) to O(n) space for brute force
1 parent eb0a554 commit 595f619

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

Python/chapter01/1.4 - PalinPerm/miguel_1.4_sol.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ def _is_palindrome(s: str) -> bool:
4242
A palindrome is a word or phrase that is the same forwards and backwards.
4343
Whitespace will not be considered when determining palindrome.
4444
This function is case insensitive.
45+
Runtime: O(n)
46+
Space Complexity: O(n)
4547
:param s: the string we check, possible permutation of a palindrome
4648
:return: true if s is a palindrome, false otherwise
4749
"""
@@ -60,16 +62,15 @@ def is_permutation_of_palindrome_brute_force(s: str) -> bool:
6062
the reversed version. So, the space will not be taken into account only when determining palindrome.
6163
Assuming ASCII
6264
Runtime is O(n!)
63-
Space complexity is O(n!)
65+
Space complexity is O(n)
6466
Given: Expect:
6567
Tact Coa True (permutations: "taco cat", "atco cta")
6668
:param s: the string that we want to check for perm of a palindrome
6769
:return: True if s is a palindrome, False otherwise.
6870
"""
6971
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)):
7374
return True
7475
return False
7576

0 commit comments

Comments
 (0)