Skip to content

Commit d8a4faf

Browse files
ashwindasrcclauss
andauthored
Update is_palindrome.py (TheAlgorithms#2025)
* Update is_palindrome.py * Update is_palindrome.py * Reuse s Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent a15f825 commit d8a4faf

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

strings/is_palindrome.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
def is_palindrome(s):
1+
def is_palindrome(s: str) -> bool:
22
"""
33
Determine whether the string is palindrome
44
:param s:
@@ -7,7 +7,16 @@ def is_palindrome(s):
77
True
88
>>> is_palindrome("Hello")
99
False
10+
>>> is_palindrome("Able was I ere I saw Elba")
11+
True
12+
>>> is_palindrome("racecar")
13+
True
14+
>>> is_palindrome("Mr. Owl ate my metal worm?")
15+
True
1016
"""
17+
# Since Punctuation, capitalization, and spaces are usually ignored while checking Palindrome,
18+
# we first remove them from our string.
19+
s = "".join([character for character in s.lower() if character.isalnum()])
1120
return s == s[::-1]
1221

1322

0 commit comments

Comments
 (0)