LeetCode Problem

How to Solve Valid Palindrome

To solve the Valid Palindrome problem, iterate over the string with two pointers from both ends, skipping non-alphanumeric characters. If characters match, continue; otherwise, return false. This solution ensures time efficiency with a time complexity of O(n).

GhostInterview Help

Need help with Valid Palindrome without spending extra time grinding it?

GhostInterview can read Valid Palindrome from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #125Two-pointer scanning with invariant trackingReviewed 2026-03-08
Difficulty
Easy
Primary pattern
Two-pointer scanning with invariant tracking
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

To solve the Valid Palindrome problem, iterate over the string with two pointers from both ends, skipping non-alphanumeric characters. If characters match, continue; otherwise, return false. This solution ensures time efficiency with a time complexity of O(n).

Problem Statement

A valid palindrome is a string that reads the same forward and backward when considering only alphanumeric characters. To determine if a given string is a palindrome, convert all uppercase characters to lowercase and remove any non-alphanumeric characters.

The task is to return true if the string is a palindrome, or false otherwise. For example, for the string "A man, a plan, a canal: Panama", the function should return true, while for "race a car", it should return false.

Examples

Example 1

Input: s = "A man, a plan, a canal: Panama"

Output: true

"amanaplanacanalpanama" is a palindrome.

Example 2

Input: s = "race a car"

Output: false

"raceacar" is not a palindrome.

Example 3

Input: s = " "

Output: true

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 * 105
  • s consists only of printable ASCII characters.

Solution Approach

Two-pointer scanning

Use two pointers, one starting at the beginning and one at the end of the string. Skip non-alphanumeric characters and compare the characters at both pointers. If they are equal, move both pointers towards the center.

Invariant tracking

Track the invariant that the characters at the left pointer must match those at the right pointer as we progress through the string. If any mismatch occurs, return false immediately.

Efficient handling of characters

Ensure case-insensitivity by converting characters to lowercase and skip non-alphanumeric characters efficiently using simple conditions, maintaining linear time complexity.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

The time complexity is O(n), where n is the length of the string. This is because we only traverse the string once with two pointers, making it efficient for large inputs. The space complexity is O(1), as we do not use extra space apart from the two pointers and character comparisons.

What Interviewers Usually Probe

  • Evaluate the candidate’s ability to implement two-pointer techniques.
  • Check if the candidate effectively handles edge cases such as empty strings or strings with non-alphanumeric characters.
  • Observe the candidate's ability to optimize both time and space for this problem.

Common Pitfalls or Variants

Common pitfalls

  • Forgetting to ignore non-alphanumeric characters, which can lead to incorrect results.
  • Not converting all characters to lowercase, causing mismatches between uppercase and lowercase letters.
  • Not handling the empty string or edge cases where the string is very short.

Follow-up variants

  • Palindromes with mixed case and special characters.
  • Strings with a large number of non-alphanumeric characters.
  • Edge case handling for strings of length 1 or 0.

How GhostInterview Helps

  • Guides candidates to implement the two-pointer technique with clear steps, ensuring proper string traversal.
  • Helps the candidate avoid common pitfalls such as missing character normalization and improper edge case handling.
  • Improves problem-solving efficiency by emphasizing the trade-off between time complexity and space complexity.

Topic Pages

FAQ

How do I check if a string is a valid palindrome?

You can check if a string is a valid palindrome by comparing characters from both ends using two pointers, skipping non-alphanumeric characters and ensuring case-insensitivity.

What is the time complexity of solving the Valid Palindrome problem?

The time complexity is O(n), where n is the length of the string, as we traverse the string once with two pointers.

What are some common mistakes when solving the Valid Palindrome problem?

Some common mistakes include not properly skipping non-alphanumeric characters, failing to convert characters to lowercase, and not handling edge cases like empty strings.

How do I handle non-alphanumeric characters in the Valid Palindrome problem?

Non-alphanumeric characters should be skipped during the comparison by checking each character and moving the pointers accordingly.

Can you explain the two-pointer technique for Valid Palindrome?

The two-pointer technique involves setting one pointer at the beginning and the other at the end of the string, comparing characters and moving towards the center, skipping non-alphanumeric characters and handling case insensitivity.

GhostInterview Solver

Need direct help with Valid Palindrome instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Valid Palindrome from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.