125. Valid Palindrome
Tags:
Easy
Skills:
String
June 24, 2025
04:32 AM
No headings found
Loading content...
Related Posts
Leetcode
No headings found
Related Posts
Leetcode
Problem
Kiểm tra xem 1 chuỗi có phải palindrome hay không
Approach - Làm sạch chuỗi rồi so sánh với chuỗi đảo ngược
Solution
1function isPalindrome(s: string): boolean {
2 const clean = s.toLowercase().replace(/[^a-z0-9]/g,"")
3 return clean = clean.split("").reverse().join("");
4};Approach - Two pointer tối ưu bộ nhớ
Solution
1function isPalindrome(s: string): boolean {
2 let i = 0, j = s.length - 1;
3 while (i < j) {
4 while (i < j && !/^[a-zA-Z0-9]/.test(s[i])) i++
5 while (i < j && !/^[a-zA-Z0-9]/.test(s[j])) j--
6 if (s[i].toLowerCase() !== s[j].toLowerCase()) return false;
7 i++
8 j--
9 }
10
11 return true;
12};