[Two pointers-facing double pointers] Lintcode 415. Effective palindrome string

Lintcode 415. Valid palindrome string

Title description: Given a string, judge whether it is a palindrome. Only letters and numbers are considered, and case is ignored.
Insert picture description here

class Solution {
    
    
public:
    /**
     * @param s: A string
     * @return: Whether the string is a valid palindrome
     */
    bool isPalindrome(string &s) {
    
    
        for (int i = 0, j = s.size() - 1; i < j; ++i, --j) {
    
    
            while (!isdigit(s[i]) && !isalpha(s[i]) && i < j) {
    
    
                ++i;
            }
            while (!isdigit(s[j]) && !isalpha(s[j]) && i < j) {
    
    
                --j;
            }
            if (i < j && tolower(s[i]) != tolower(s[j])) {
    
    
                return false;
            }
        }
        return true;
    }
};

Guess you like

Origin blog.csdn.net/phdongou/article/details/113838158