Wednesday, 5 October 2016

Valid Palindrome (LeetCode)

Problem: Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases

class Solution {
public:
    bool isPalindrome(string s) {
        
        bool isEmpty=true;
        for(int i=0;i<s.size();i++)
            if(isalnum(s[i]))
            {
                isEmpty=false;
                break;
            }
            
        if(isEmpty) return 1;
        
        int left = 0, right = s.size()-1;
        while(left<right)
        {
            while(!isalnum(s[left])) left++;
            while(!isalnum(s[right])) right--;
            if(tolower(s[left])!=tolower(s[right])) return 0;
            left++;
            right--;
        }
        return 1;
    }
};

No comments:

Post a Comment