Watch 10 video solutions for Valid Palindrome, a easy level problem involving Two Pointers, String. This walkthrough by NeetCode has 318,354 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.
Given a string s, return true if it is a palindrome, or false otherwise.
Example 1:
Input: s = "A man, a plan, a canal: Panama" Output: true Explanation: "amanaplanacanalpanama" is a palindrome.
Example 2:
Input: s = "race a car" Output: false Explanation: "raceacar" is not a palindrome.
Example 3:
Input: s = " " Output: true Explanation: s is an empty string "" after removing non-alphanumeric characters. Since an empty string reads the same forward and backward, it is a palindrome.
Constraints:
1 <= s.length <= 2 * 105s consists only of printable ASCII characters.Problem Overview: You receive a string that may contain letters, digits, spaces, and punctuation. Determine whether it reads the same forward and backward after ignoring non‑alphanumeric characters and treating uppercase and lowercase letters as equal.
Approach 1: String Normalization and Reverse Comparison (O(n) time, O(n) space)
The straightforward solution is to first normalize the string. Iterate through each character, keep only alphanumeric characters, and convert them to lowercase. Store the cleaned characters in a new string or array. Once normalized, check whether this string equals its reverse using reverse() or slicing like s[::-1] in Python. This works because the preprocessing step removes everything that should be ignored by the palindrome check. The tradeoff is extra memory since you build a new string of size up to n, giving O(n) space complexity.
This method is easy to implement and highly readable. If clarity matters more than memory usage, or if you're quickly validating input before other processing, normalization followed by reverse comparison is perfectly acceptable. The approach mainly relies on basic string manipulation operations.
Approach 2: Two-Pointer Technique (O(n) time, O(1) space)
The optimal solution avoids building a new string. Instead, use two indices: one starting at the beginning of the string and one at the end. Move both pointers toward the center while skipping characters that are not alphanumeric. When both pointers land on valid characters, compare them after converting to lowercase. If they differ, the string cannot be a palindrome. If they match, continue moving inward until the pointers cross.
This method performs a single pass through the string, so the time complexity remains O(n). However, it uses constant extra memory because it compares characters directly in the original string. The logic is a classic application of two pointers, where symmetric positions are validated without allocating additional structures.
The key insight is that characters that don't affect the palindrome condition can simply be skipped during traversal. Instead of preprocessing the entire string, filtering happens on the fly. This reduces memory usage and keeps the algorithm efficient even for large inputs.
Recommended for interviews: Interviewers expect the Two-Pointer Technique. The normalization approach shows that you understand the problem constraints, but the two-pointer version demonstrates stronger algorithmic thinking and space optimization. It also appears frequently in other string and two‑pointer interview problems where symmetric comparisons are required.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| String Normalization + Reverse Comparison | O(n) | O(n) | When readability matters and extra memory is acceptable |
| Two-Pointer Technique | O(n) | O(1) | Best for interviews and large inputs where constant space is preferred |