[HAI] week3 two pointer palindrome
https://leetcode.com/problems/valid-palindrome/description/
This commit is contained in:
@@ -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;
|
||||
};
|
||||
Reference in New Issue
Block a user