↻one more pass.
Two pointers · Easy

Valid Palindrome: the idea to remember.

Ignoring nonalphanumeric ASCII characters and letter case, decide whether a string reads the same both ways.

Solve on LeetCode ↗

The core insight

Skip ignored characters at both ends and compare the remaining pair.

Why it works

Every compared outer pair matches. Removing it preserves the palindrome question for the remaining interval.

The mistake to avoid

Check bounds while skipping punctuation, including strings consisting only of punctuation.

Time: O(n)Space: O(1)

Commented Java solution

Use java.util.* imports and a surrounding solution class. Tree snippets assume the standard TreeNode definition; linked lists use ListNode and Clone Graph uses a Node with val and neighbors; Copy Random List uses Node with val, next and random. Read the input assumptions above.

boolean isPalindrome(String s) {
    int left = 0, right = s.length() - 1;
    while (left < right) {
        while (left < right && !Character.isLetterOrDigit(s.charAt(left))) left++;
        while (left < right && !Character.isLetterOrDigit(s.charAt(right))) right--;
        if (Character.toLowerCase(s.charAt(left)) !=
            Character.toLowerCase(s.charAt(right))) return false;
        left++;
        right--;
    }
    return true;
}

Make the next connection

Independent revision notes with credited Java references. Not affiliated with LeetCode or the source sheets.