4/14/2014

Leetcode -- Valid Palindrome

 class Solution {  
 public:  
   bool isPalindrome(string s) {  
     int start = 0;  
     int end = s.length() - 1;  
     while(start < end) {  
       while(!isAlpha(s[start]) && start < s.length()){  
         start++;  
       }  
       while(!isAlpha(s[end]) && end >= 0) {  
         end--;  
       }  
       if(start < s.length() && end >= 0 && !iEqual(s[start], s[end])) {  
         return false;  
       } else {  
         start++;  
         end--;  
       }  
     }  
     return true;  
   }  
   bool isAlpha(char c) {  
     return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9');  
   }  
   bool iEqual(char c1, char c2) {  
     if (c1 >= 'A' && c1 <= 'Z') {  
       c1 = c1 + ('a' - 'A');  
     }  
     if (c2 >= 'A' && c2 <= 'Z') {  
       c2 = c2 + ('a' - 'A');  
     }  
     return c1 == c2;  
   }  
 };  

No comments: