Skip to content

Commit 8255243

Browse files
committed
Valid Palindrome
1 parent 8cdb2dc commit 8255243

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

valid-palindrome/sunjae95.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @description
3+
* brainstorming:
4+
* string method + two pointer
5+
*
6+
* time complexity: O(n)
7+
* space complexity: O(n)
8+
*/
9+
10+
var isPalindrome = function (s) {
11+
const asciiArray = [];
12+
13+
for (let i = 0; i < s.length; i++) {
14+
const asciiCode = s[i].toLowerCase().charCodeAt(0);
15+
const isNumber = asciiCode >= 48 && asciiCode <= 57;
16+
const isLowerCase = asciiCode >= 97 && asciiCode <= 122;
17+
18+
if (isNumber || isLowerCase) asciiArray.push(asciiCode);
19+
}
20+
21+
const len = asciiArray.length;
22+
const middleIndex = Math.floor(len);
23+
24+
for (let i = 0; i < middleIndex; i++) {
25+
if (asciiArray[i] !== asciiArray[len - i - 1]) return false;
26+
}
27+
28+
return true;
29+
};

0 commit comments

Comments
 (0)