Sharing my 12ms C++ solution


  • 0
    T
    class Solution {
    private:
        bool convert(char& c)
        {
            if((c>='a'&&c<='z') || (c>='0' && c<='9'))
            {
                return true;
            }
            else if(c>='A' && c<='Z')
            {
                c = c-'A'+'a';
                return true;
            }
            else
            {
                return false;
            }
        }
        
    public:
        bool isPalindrome(string s) {
            int left = 0;
            int right = s.length()-1;
            while(left<right)
            {
                if(convert(s[left])==false)
                {
                    left++;
                    continue;
                }
                if(convert(s[right])==false)
                {
                    right--;
                    continue;
                }
                
                if(s[left]==s[right])
                {
                    left++;
                    right--;
                }
                else
                    return false;
            }
            
            return true;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.