leetcode - Valid Palindrome
leetcode - 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.
1 class Solution { 2 public: 3 bool isAlphanumeric(const char a){ 4 if( (a >= 'a' && a <= 'z') || (a >= 'A' && a <= 'Z') || (a >= '0' && a <= '9' )) 5 return true; 6 else 7 return false; 8 } 9 bool isPalindrome(string s) { 10 if(s.size()==0) return true; 11 string::iterator it_beg = s.begin(); 12 string::iterator it_end = s.end(); 13 //bool flag = true; 14 //while(!isAlphanumeric(*it_beg) && it_end!=s.end()) it_beg++; 15 //while(!isAlphanumeric(*it_end) && it_end!=s.begin()) it_end--; 16 while(it_beg < it_end){ 17 if(!isAlphanumeric(*it_beg)) it_beg++; 18 else if(!isAlphanumeric(*it_end)) it_end--; 19 else if(*it_beg == *it_end || (*it_beg -*it_end == 'a' - 'A') || (*it_beg -*it_end == 'A' - 'a') ){ 20 it_beg++; 21 it_end--; 22 } 23 else{ 24 return false; 25 } 26 } 27 return true; 28 } 29 };