qiaoxu123
2/26/2019 - 12:34 AM

125. Valid Palindrome

//Runtime: 8 ms, faster than 99.85%
//Memory Usage: 9.1 MB, less than 77.05%

class Solution {
public:
    bool isPalindrome(string s) {
    for (int i = 0, j = s.size() - 1; i < j; i++, j--) { // Move 2 pointers from each end until they collide
        while (isalnum(s[i]) == false && i < j) i++; // Increment left pointer if not alphanumeric
        while (isalnum(s[j]) == false && i < j) j--; // Decrement right pointer if no alphanumeric
        if (toupper(s[i]) != toupper(s[j])) return false; // Exit and return error if not match
    }
    
    return true;
    }
};