125. Valid Palindrome
1. Description
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
2. Example
Example 1:
Input: “A man, a plan, a canal: Panama”
Output: true
Example 2:
Input: “race a car”
Output: false
3. Constraints
- s consists only of printable ASCII characters.
4. Solutions
My Accepted Solution
n = i_str.size()
Time complexity: O(n)
Space complexity: O(1)
class Solution {
public:
bool isPalindrome(const string &str) {
for (int left = 0, right = str.size() - 1; left < right; ++left, --right) {
while (!isalnum(str[left]) && left < right) {
++left;
}
while (!isalnum(str[right]) && left < right) {
--right;
}
if (left < right && tolower(str[left]) != tolower(str[right])) {
return false;
}
}
return true;
}
};