File tree Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change
1
+ var isPalindrome = function ( s ) {
2
+ let left = 0 , right = s . length - 1 ;
3
+ while ( left < right ) {
4
+ while ( left < right && ! isAlphaNumeric ( s [ left ] ) ) { // increment the left index until it's an alphanumeric character
5
+ left ++ ;
6
+ }
7
+ while ( left < right && ! isAlphaNumeric ( s [ right ] ) ) { // decrement the right index until it's an alphanumeric character
8
+ right -- ;
9
+ }
10
+ if ( s [ left ++ ] . toLowerCase ( ) !== s [ right -- ] . toLowerCase ( ) ) return false ; // compare the two string values, if different return false;
11
+ }
12
+ return true ;
13
+ } ;
14
+
15
+ function isAlphaNumeric ( char ) { // use ASCII code to findout if char is alphanumeric or not
16
+ const asciiCode = char . charCodeAt ( 0 ) ;
17
+ return ( asciiCode >= 65 && asciiCode <= 90 ) ||
18
+ ( asciiCode >= 97 && asciiCode <= 122 ) ||
19
+ ( asciiCode >= 48 && asciiCode <= 57 )
20
+ }
21
+
22
+ // time - O(n) iterate through the string once
23
+ // space - O(1) no extra space created
You can’t perform that action at this time.
0 commit comments