Skip to main content

8. Valid Palindrome

easyAsked at Postman

Determine whether a string is a palindrome after stripping non-alphanumerics and lowercasing.

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

Problem

Given a string s, consider only alphanumeric characters and ignore case. Return true if it reads the same forward and backward.

Constraints

  • 1 <= s.length <= 2 * 10^5
  • s consists 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 compare

Strip non-alphanumerics, lowercase, then compare against the reverse.

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

Tradeoff:

2. Two-pointer in place

Walk pointers from both ends, skipping non-alphanumerics; compare characters lowercased.

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

Tradeoff:

Postman-specific tips

Postman favors O(1)-extra-memory passes since URL and header normalization at scale must avoid extra string allocations on hot paths.

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 Postman interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →