# 125. Valid Palindrome

• Time: $O(n)$
• Space: $O(1)$
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 class Solution { public: bool isPalindrome(string s) { int l = 0; int r = s.length() - 1; while (l < r) { while (l < r && !isalnum(s[l])) ++l; while (l < r && !isalnum(s[r])) --r; if (tolower(s[l]) != tolower(s[r])) return false; ++l; --r; } return true; } }; 
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 class Solution { public boolean isPalindrome(String s) { int l = 0; int r = s.length() - 1; while (l < r) { while (l < r && !Character.isLetterOrDigit(s.charAt(l))) ++l; while (l < r && !Character.isLetterOrDigit(s.charAt(r))) --r; if (Character.toLowerCase(s.charAt(l)) != Character.toLowerCase(s.charAt(r))) return false; ++l; --r; } return true; } } 
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class Solution: def isPalindrome(self, s: str) -> bool: l = 0 r = len(s) - 1 while l < r: while l < r and not s[l].isalnum(): l += 1 while l < r and not s[r].isalnum(): r -= 1 if s[l].lower() != s[r].lower(): return False l += 1 r -= 1 return True