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
Companies:
Facebook, Microsoft, Apple
Related Topics:
Two Pointers, String
Similar Questions:
// OJ: https://leetcode.com/problems/valid-palindrome/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(1)
class Solution {
public:
bool isPalindrome(string s) {
int i = 0, j = s.size();
while (i < j) {
while (i < j && !isalnum(s[i])) ++i;
while (i < j && !isalnum(s[j])) --j;
if (i < j && tolower(s[i++]) != tolower(s[j--])) return false;
}
return true;
}
};