mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +00:00
Update is_palindrome.py (#8022)
This commit is contained in:
parent
9f041e9cc8
commit
32a1ff9359
|
@ -16,7 +16,24 @@ def is_palindrome(s: str) -> bool:
|
|||
# Since punctuation, capitalization, and spaces are often ignored while checking
|
||||
# palindromes, we first remove them from our string.
|
||||
s = "".join(character for character in s.lower() if character.isalnum())
|
||||
return s == s[::-1]
|
||||
# return s == s[::-1] the slicing method
|
||||
# uses extra spaces we can
|
||||
# better with iteration method.
|
||||
|
||||
end = len(s) // 2
|
||||
n = len(s)
|
||||
|
||||
# We need to traverse till half of the length of string
|
||||
# as we can get access of the i'th last element from
|
||||
# i'th index.
|
||||
# eg: [0,1,2,3,4,5] => 4th index can be accessed
|
||||
# with the help of 1st index (i==n-i-1)
|
||||
# where n is length of string
|
||||
|
||||
for i in range(end):
|
||||
if s[i] != s[n - i - 1]:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
Loading…
Reference in New Issue
Block a user