Skip to content

Commit 4f956d6

Browse files
committed
valid-palindrome
1 parent 5dbcda1 commit 4f956d6

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

valid-palindrome/jun0811.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var isPalindrome = function (s) {
2+
let left = 0;
3+
let right = s.length - 1;
4+
5+
while (left < right) {
6+
while (left < right && !isAlphaNumeric(s[left])) {
7+
left++;
8+
}
9+
10+
while (left < right && !isAlphaNumeric(s[right])) {
11+
right--;
12+
}
13+
14+
if (s[left].toLowerCase() !== s[right].toLowerCase()) {
15+
return false;
16+
}
17+
18+
left++;
19+
right--;
20+
}
21+
22+
return true;
23+
};
24+
25+
function isAlphaNumeric(char) {
26+
const code = char.charCodeAt(0);
27+
return (
28+
(code >= 48 && code <= 57) || // 0-9
29+
(code >= 65 && code <= 90) || // A-Z
30+
(code >= 97 && code <= 122)
31+
); // a-z
32+
}

0 commit comments

Comments
 (0)