EASY NC#10 Blind #56 Two Pointers

125. Valid Palindrome

๐Ÿ“– Problem

Given a string s, return true if it is a palindrome, or false otherwise. 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.

๐Ÿง  Visual Learning Aid

1 Model the input into the right structure
2 Choose the core technique and invariant
3 Execute step-by-step with a sample
4 Validate complexity and edge cases

JS/TS Refreshers

  • โ€ขArray methods (`push`, `pop`, `shift`, `slice`)
  • โ€ขObject/Map/Set usage patterns
  • โ€ขFunction parameter and return typing
  • โ€ขIn-place array updates
  • โ€ขSorted array traversal
  • โ€ขBoundary condition checks

Logical Thinking Concepts

  • โ€ขDefine invariants before coding
  • โ€ขCheck edge cases first (`[]`, single element, duplicates)
  • โ€ขEstimate time/space before implementation
  • โ€ขApply Two Pointers reasoning pattern

๐Ÿ’ก Approach

  • โ†’ Use two pointers from both ends
  • โ†’ Move pointers inward while matching
  • โ†’ Skip non-alphanumeric characters
  • โ†’ Ignore case
  • โ†’ Time: O(n), Space: O(1)

๐Ÿ› ๏ธ Hints & Pitfalls

Hints

  • โ€ขUse two pointers from both ends
  • โ€ขMove pointers inward while matching
  • โ€ขSkip non-alphanumeric characters

Common Pitfalls

  • โ€ขIgnore case
  • โ€ขTime: O(n), Space: O(1)

๐Ÿงช Test Cases

Hidden tests on submit: 5

Test Case 1
Not run
Input:
isPalindrome('A man, a plan, a canal: Panama');
Expected:
true
Test Case 2
Not run
Input:
isPalindrome('');
Expected:
true
Test Case 3
Not run
Input:
isPalindrome('a');
Expected:
true

๐Ÿ“ Code Editor

๐Ÿ“š Reference Solution

โ–ผ
โŒ˜K Search โŒ˜โ†ฉ Run โŒ˜S Submit