Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,"A man, a plan, a canal: Panama"
is a palindrome."race a car"
is not a palindrome.
Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.
For the purpose of this problem, we define empty string as valid palindrome.
Solution:
class Solution { public: bool isPalindrome(string s) { if(s.size() == 0) return true; int end = s.size() -1; int start = 0; while(start < end){ while(start < end &&!isAlphanum(s[start])) start++; while(start < end &&!isAlphanum(s[end])) end--; if((s[start] != s[end])){ return false; } start++; end--; } return true; } bool isAlphanum(char &a){ if(a <= '9' && a >= '0') return true; if(a <= 'z' && a >= 'a') return true; if(a <= 'Z' && a >= 'A'){ a += 32; return true; } return false; } };