Skip to content

Commit cf8bb29

Browse files
Jaehyeon Robert HanJaehyeon Robert Han
authored andcommitted
valid-palindrome solution
1 parent cd0087a commit cf8bb29

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

valid-palindrome/Zioq.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
var isPalindrome = function(s) {
6+
7+
let text = s.replace(/[^0-9a-z]/gi, '');
8+
text = text.replace(/\s/g, '').toLowerCase();
9+
10+
const str_arr = text.split("");
11+
if( str_arr % 2 === 1 ) {
12+
return false;
13+
}
14+
15+
let length = str_arr.length - 1 ;
16+
for ( const [i, value] of str_arr.entries()) {
17+
if( value == str_arr[length -i] ) continue;
18+
if(value != str_arr[length - i]) {
19+
return false
20+
}
21+
}
22+
return true;
23+
};
24+
25+
/*
26+
Space Complexity - O(n) - Create a array to store elements
27+
Time Complexity - O(n) - Traverse through the array
28+
*/
29+
30+
/* Test code */
31+
console.log(isPalindrome("A man, a plan, a canal: Panama")); // true
32+
console.log(isPalindrome("race a car")); // false
33+
console.log(isPalindrome(" ")); // true

0 commit comments

Comments
 (0)