QuestionsDSA

Valid Palindrome

Two PointersEasyDSA

Given a string, return true if it reads the same forwards and backwards, considering only alphanumeric characters and ignoring case.

What it tests

Recognizing that comparing ends inward avoids extra space.

Approach & answer

One pointer at each end. Skip non-alphanumerics, compare lowercased characters, move inward. O(1) extra space — better than reversing the string or filtering into a new array first. The two-pointers-converging shape works because a palindrome is symmetric: position i must mirror position n−1−i. The fiddly part is the skip logic — advance each pointer past junk independently before comparing, and guard `i < j` inside the skip loops so pointers never cross. This same converge-from-both-ends idea underlies reversing in place and the sorted-array two-sum.

Use this technique when

Comparing a sequence against itself from both ends, or scanning a sorted array for a condition.

Complexity

Time O(n) · Space O(1)

References

js