My C++ solution, feedback is welcome


  • 0
    D
        bool isPalindrome(string s) {
    	if (!s.compare("")) return true;
    
    	for (int i = 0, j = s.length() - 1; j >= 0 && i < s.length();) {
    		if (!isalpha(s[i]) && !isdigit(s[i])) // if not alphabetic increment
    			i++;
    		else
    		if (!isalpha(s[j]) && !isdigit(s[j])) // if not alphabetic increment
    			j--;
    		else {
    			if (tolower(s[i]) != tolower(s[j]))
    				return false;
    			i++; j--;
    		}
    	}
    	return true;
    }
    

    IMO this turned out to be very short, but I'd like feedback on if I can improve this further or not.


  • 0
    S
    bool isPalindrome(string s) {
        int start = 0, end = s.size()-1;
        while (start < end) {
            if (!isalnum(s[start]))
                start++;
            else if (!isalnum(s[end]))
                end--;
            else if (tolower(s[start]) != tolower(s[end]))
                return false;
            else {
                start ++;
                end --;
            }
        }
        return true;
    }

  • 0
    T
    This post is deleted!

Log in to reply
 

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