File tree 1 file changed +48
-0
lines changed
solution/125.Valid Palindrome
1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change
1
+ const isPalindrome1 = function ( s ) {
2
+ let arr1 = [ ] , arr2 = [ ] ;
3
+ for ( let i = 0 ; i < s . length ; i ++ ) {
4
+ if ( s [ i ] >= 'A' && s [ i ] <= 'Z' ) {
5
+ arr1 . push ( s [ i ] . toLowerCase ( ) ) ;
6
+ }
7
+ if ( s [ i ] >= '0' && s [ i ] <= '9' ||
8
+ s [ i ] >= 'a' && s [ i ] <= 'z' ) {
9
+ arr1 . push ( s [ i ] ) ;
10
+ }
11
+ }
12
+ arr2 = [ ...arr1 ] ;
13
+ arr2 . reverse ( ) ;
14
+ return arr1 . join ( '' ) === arr2 . join ( '' ) ;
15
+ }
16
+
17
+ const isPalindrome = function ( s ) {
18
+ function isNumOrAl ( a ) {
19
+ if ( a >= 'A' && a <= 'Z' ||
20
+ a >= '0' && a <= '9' ||
21
+ a >= 'a' && a <= 'z' ) {
22
+ return true ;
23
+ }
24
+ else {
25
+ return false ;
26
+ }
27
+ }
28
+
29
+ if ( s . length === 0 ) {
30
+ return true ;
31
+ }
32
+ let i = 0 , j = s . length - 1 ;
33
+ while ( i < j ) {
34
+ while ( i < j && ! isNumOrAl ( s [ i ] ) ) {
35
+ i ++ ;
36
+ }
37
+ while ( i < j && ! isNumOrAl ( s [ j ] ) ) {
38
+ j -- ;
39
+ }
40
+ if ( s [ i ] . toLowerCase ( ) !== s [ j ] . toLowerCase ( ) ) {
41
+ return false ;
42
+ } else {
43
+ i ++ ;
44
+ j -- ;
45
+ }
46
+ }
47
+ return true ;
48
+ } ;
You can’t perform that action at this time.
0 commit comments