Short Straightforward Solution


  • 2
    L
    public bool IsPalindrome(string s) {
        for(int i = 0, j = s.Length - 1; i <= j;)
            if(!isAlphebet(s[i])) i++;
            else if(!isAlphebet(s[j])) j--;
            else if(Char.ToLower(s[i]) == Char.ToLower(s[j])) { i++; j--; }
            else return false;
        return true;
    }
    
    private bool isAlphebet(char ch){
        if('a' <= ch && ch <= 'z') return true;
        else if('A' <= ch && ch <= 'Z') return true;
        else if('0' <= ch && ch <= '9') return true;
        return false;
    }

Log in to reply
 

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