Skip to content

Commit 149d938

Browse files
authored
Create 125-valid-palindrome.js
1 parent 5738ba8 commit 149d938

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

125-valid-palindrome.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
const isPalindrome = function(s) {
6+
let start = 0
7+
let end = s.length - 1
8+
9+
let char = ''
10+
while(start < end) {
11+
while(start < s.length && !valid(s[start])) {
12+
start++
13+
}
14+
while(end >=0 && !valid(s[end])) {
15+
end--
16+
}
17+
if(start < s.length && end >=0) {
18+
if(s[start].toLowerCase() !== s[end].toLowerCase()) return false
19+
}
20+
21+
start++
22+
end--
23+
}
24+
25+
return true
26+
};
27+
28+
function valid(c) {
29+
const code = c.toLowerCase().charCodeAt(0)
30+
const zeroCode = ('0').charCodeAt(0)
31+
const nineCode = ('9').charCodeAt(0)
32+
const aCode = ('a').charCodeAt(0)
33+
const zCode = ('z').charCodeAt(0)
34+
if( (code >= zeroCode && code <= nineCode) || ( code >= aCode && code <= zCode ) ) return true
35+
36+
return false
37+
}

0 commit comments

Comments
 (0)