125. Valid Palindrome
https://leetcode.com/problems/valid-palindrome/
Problem
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: trueExample 2:
Input: "race a car"
Output: falseConstraints:
sconsists only of printable ASCII characters.
class Solution {
public:
bool isPalindrome(const string s) {
int left = 0, right = s.size() - 1;
while (left < right) {
while (left < s.size() && !isalpha(s[left]) && !isdigit(s[left])) ++left;
while (right >= 0 && !isalpha(s[right]) && !isdigit(s[right])) --right;
if (left >= right) break;
if (tolower(s[left++]) != tolower(s[right--])) return false;
}
return true;
}
};#string
Last updated
Was this helpful?