Skip to content

Commit 653f199

Browse files
committed
Valid Palindrome
1 parent 08497f1 commit 653f199

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

valid-palindrome/casentino.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
function isPalindrome(s: string): boolean {
2+
let start = 0;
3+
let last = s.length - 1;
4+
while (start < last) {
5+
while (start < last && !isAlphanumeric(s.charAt(start))) {
6+
start += 1;
7+
}
8+
while (start < last && !isAlphanumeric(s.charAt(last))) {
9+
last -= 1;
10+
}
11+
if (s.charAt(start).toLowerCase() !== s.charAt(last).toLowerCase()) {
12+
return false;
13+
}
14+
start += 1;
15+
last -= 1;
16+
}
17+
return true;
18+
}
19+
20+
function isAlphanumeric(character: string) {
21+
const characterCode = character.charCodeAt(0);
22+
const lowerStart = "a".charCodeAt(0);
23+
const lowerEnd = "z".charCodeAt(0);
24+
const upperStart = "A".charCodeAt(0);
25+
const upperEnd = "Z".charCodeAt(0);
26+
const numericStart = "0".charCodeAt(0);
27+
const numericEnd = "9".charCodeAt(0);
28+
if (upperStart <= characterCode && upperEnd >= characterCode) {
29+
return true;
30+
}
31+
if (lowerStart <= characterCode && lowerEnd >= characterCode) {
32+
return true;
33+
}
34+
if (numericStart <= characterCode && numericEnd >= characterCode) {
35+
return true;
36+
}
37+
return false;
38+
}

0 commit comments

Comments
 (0)