Saturday, September 14, 2013

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.
bool isPalin(string r)
{
    int i  =0;
    int j = r.size() - 1;
    while(i < j)
    {
               
            if(r[i] != r[j])
                return false;
    
        i++;
        j--;
    }
    
    return true
    ;
}
 
bool isPalindrome(string s) {

    if(s.size() <=0 )
        return true;
        
    string r = "";
    for(int i =0; i < s.size(); i++)
    {
         if(
            (s[i] >= 'a'  && s[i] <= 'z')
                        ||
            (s[i] >= '1'  && s[i] <= '9')
            )
         {
             r = r + s[i];
         }
        
        if(                        
           (s[i] >= 'A'  && s[i] <= 'Z')

           )
        {
            char t  = ('a' + s[i] - 'A');
            r = r + t;
        }
    }
    
    
    return isPalin(r);
        
    
}

No comments:

Post a Comment