https://leetcode.com/problems/valid-palindrome/?envType=study-plan-v2&envId=top-interview-150
def isPalindrome(self, s: str) -> bool:
left, right = 0, len(s) - 1
while left < right:
# move left pointer to the next alphanumeric character
while left < right and not s[left].isalnum():
left += 1
# move right pointer to the previous alphanumeric character
while left < right and not s[right].isalnum():
right -= 1
# compare characters (case insensitive)
if s[left].lower() != s[right].lower():
return False
left += 1
right -= 1
return True
class Solution {
public:
bool isPalindrome(string s) {
int left = 0, right = s.size();
while(left < right){
// Move left pointer to the next alphanumeric character
while(left < right && !isalnum(s[left])){
++left;
}
// Move right pointer to the previous alphanumeric character
while(left < right && !isalnum(s[right])){
--right;
}
// Compare characters (case insensitive)
if(tolower(s[left]) != tolower(s[right])){
return false;
}
++left;
--right;
}
return true;
}
};
$$ \text{Time}=O(N)\\ \text{Space}=O(1) $$