File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed
Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param {string } s
3+ * @return {boolean }
4+ */
5+ var isPalindrome = function ( s ) {
6+
7+ let text = s . replace ( / [ ^ 0 - 9 a - 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
You can’t perform that action at this time.
0 commit comments