Skip to main content

13. Valid Palindrome

easyAsked at Yelp

Determine whether a string is a palindrome after stripping non-alphanumerics and lower-casing — Yelp uses this to test two-pointer fluency before scaling to review-fraud near-duplicate detection.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Problem

Given a string s, return true if it is a palindrome after converting all uppercase letters to lowercase and removing all non-alphanumeric characters. An empty string is a palindrome.

Constraints

  • 1 <= s.length <= 2 * 10^5
  • s consists only of printable ASCII characters

Examples

Example 1

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

Example 2

Input
s = "race a car"
Output
false

Approaches

1. Clean and reverse

Filter then compare the cleaned string to its reverse.

Time
O(n)
Space
O(n)
const c = s.toLowerCase().replace(/[^a-z0-9]/g, '');
return c === c.split('').reverse().join('');

Tradeoff:

2. Two pointers

Walk inward from both ends, skipping non-alphanumerics and comparing case-insensitively.

Time
O(n)
Space
O(1)
function isPalindrome(s) {
  const ok = (c) => /[a-z0-9]/i.test(c);
  let l = 0, r = s.length - 1;
  while (l < r) {
    while (l < r && !ok(s[l])) l++;
    while (l < r && !ok(s[r])) r--;
    if (s[l].toLowerCase() !== s[r].toLowerCase()) return false;
    l++; r--;
  }
  return true;
}

Tradeoff:

Yelp-specific tips

Yelp interviewers will pivot to review fraud — be ready to discuss how a normalized-text comparison generalizes to detecting near-duplicate spam reviews across business listings.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

Practice these live with InterviewChamp.AI

Drill Valid Palindrome and other Yelp interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →