🍪 TechCookies
HomeDSASystem DesignJSMy Progress
Free
Log inStart free
TechCookies — Practice · Learn · PrepareTechCookies — Practice · Learn · Prepare
ConceptsPracticeSD challengesPricingPrivacyTermsContact
© 2026 TechCookies
DSA›Big O & Complexity›LC 125 Valid Palindrome
easyExpert mode
LC 125Valid Palindrome
easy⟺ Big O & ComplexityStringTwo Pointers
Pattern match🔗 Concept powering this problem
Ω
Big O & Complexity
0 of 10 sections complete
↗
📖 Review concept first
🎯 Pattern recognition check
Before coding: identify why big o & complexity works here.
Why algorithm efficiency matters. Time vs space tradeoffs and how to read Big-O notation.
Problem
Check whether a string is a valid palindrome, considering only letters and digits (and treating uppercase and lowercase as the same). Strip everything else out — punctuation, spaces, symbols — then check if what remains reads the same forwards and backwards.
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
// After removing non-alphanumeric characters, s is an empty string — which reads the same forward and backward.
Constraints
  • 1 ≤ s.length ≤ 2 × 10⁵
  • s consists only of printable ASCII characters
⚡Focus on reducing time complexity from the naive solution.
TypeScript⚡
JavaScript
JavaPro
PythonPro
GoPro
loading compiler…
Loading editor…
🧪 Test cases4 tests · not run
▾
Test 1
s = "A man, a plan, a canal: Panama"
Expected: true
Test 2
s = "race a car"
Expected: false
Test 3
s = " "
Expected: true
Test 4
s = "0P"
Expected: false
Target complexity for the optimal solution to this problem.
⏱ Time complexity
O(n)
Two pointers from both ends — each character visited at most once.
💾 Space complexity
O(1)
Only two index variables — no cleaned-up copy of the string.
Difficulty
easy
Concept map
Big O & Complexity↗
Problems
3 problems · big o pattern
LC 1Two SumeasyLC 217Contains DuplicateeasyLC 125Valid Palindromeeasy
Browse all problems →
✓ Accepted · 0:00 · Solutions unlocked