[HAI] week3 two pointer palindrome

https://leetcode.com/problems/valid-palindrome/description/
This commit is contained in:
2024-12-05 22:01:21 +03:00
parent f16f797381
commit 35c77a3a23
4 changed files with 157 additions and 0 deletions
+30
View File
@@ -0,0 +1,30 @@
function isLetter(c: string) {
if (/\d/.test(c)) return true;
return c.toLowerCase() != c.toUpperCase();
}
export function isPalindrome(s: string): boolean {
let L = 0;
let R = s.length - 1;
while (L <= R) {
let lChar = s[L].toLowerCase();
let rChar = s[R].toLowerCase();
if (!isLetter(lChar)) {
L++;
continue;
}
if (!isLetter(rChar)) {
R--;
continue;
}
if (lChar !== rChar) return false;
L++;
R--;
}
return true;
};