Valid Palindrome

code


class Solution {
public:
    bool isPalindrome(string s) {
        if(s.length() <= 1)
        {
            return true;
        }
        string rs;
        for(char c : s)
        {
            if(isValid(c))
            {
                if(c >= 'a')
                {
                    c -= 'a' - 'A';
                }
                rs.push_back(c);
            }
        }

        int i = 0;
        int j = rs.length()-1;
        //cout<<rs<<endl;
        while(i < j)
        {
            if(rs.substr(i, 1) != rs.substr(j, 1))
            {
                return false;
            }
            i++;
            j--;
        }

        return true;
    }

    bool isValid(const char& c)
    {
        if(c >= 'a' && c <= 'z')
        {
            return true;
        }

        if(c >= 'A' && c <= 'Z')
        {
            return true;
        }

        if(c >= '0' && c <= '9')
        {
            return true;
        }

        return false;
    }
};

results matching ""

    No results matching ""