Skip to content

Commit bd99c28

Browse files
author
bhan
committed
valid palindrome solution
1 parent b84cb92 commit bd99c28

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

valid-palindrome/byol-han.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
6+
// 1. Two Pointers
7+
// time complexity: O(n)
8+
// space complexity: O(1)
9+
var isPalindrome = function (s) {
10+
let sRefine = s.toLowerCase().replace(/[^a-z0-9]/g, "");
11+
let left = 0;
12+
let right = sRefine.length - 1;
13+
14+
while (left < right) {
15+
if (sRefine[left] !== sRefine[right]) {
16+
return false;
17+
}
18+
left++;
19+
right--;
20+
}
21+
22+
return true;
23+
};
24+
25+
// 2. String Manipulation
26+
// time complexity: O(n)
27+
// space complexity: O(n)
28+
var isPalindrome = function (s) {
29+
let refined = s.toLowerCase().replace(/[^a-z0-9]/g, "");
30+
let reversed = refined.split("").reverse().join("");
31+
return refined === reversed;
32+
};

0 commit comments

Comments
 (0)