125. Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
Note: For the purpose of this problem, we define empty string as valid palindrome.
Example 1:
Input: "A man, a plan, a canal: Panama"
Output: true
Example 2:
Input: "race a car"
Output: false
bool isPalindrome(string s) { // time: O(n); space: O(1)
for (int l = 0, r = s.length() - 1; l < r; ++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;
}
return true;
}
Last updated
Was this helpful?