Problem¶
A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.
Given a string s
, return true
if it is a palindrome, or false
otherwise.
Example 1:
**Input:** s = "A man, a plan, a canal: Panama"
**Output:** true
**Explanation:** "amanaplanacanalpanama" is a palindrome.
Solve¶
Do as the problem ask, without any optimization:
- Lower key all character,
- Keep all alphabet and numbers character only, storing in a new variable call
alphabel_only
- Check if
alphabel_only
backward and forward is the same
class Solution:
def isPalindrome(self, s: str) -> bool:
s = s.lower()
alphabel_only = ""
for c in s:
if ord("a") <= ord(c) <= ord("z") or ord("0") <= ord(c) <= ord("9") :
alphabel_only += c
for i, c in enumerate(alphabel_only):
if alphabel_only[len(alphabel_only) - 1 - i] != c:
return False
return True
Last update :
October 13, 2023
Created : August 16, 2023
Created : August 16, 2023