File tree 1 file changed +16
-14
lines changed
1 file changed +16
-14
lines changed Original file line number Diff line number Diff line change @@ -64,22 +64,24 @@ var isPalindrome = function(s) {
64
64
* @return {boolean }
65
65
*/
66
66
var isPalindrome = function ( s ) {
67
- const isAlpha = c => ( c . toLowerCase ( ) >= 'a' && c . toLowerCase ( ) <= 'z' ) || c >= '0' && c <= '9'
67
+ const isAlphaNumeric = c => ( c . toLowerCase ( ) >= 'a' && c . toLowerCase ( ) <= 'z' ) || c >= '0' && c <= '9'
68
68
69
- let i = 0 ;
70
- let j = s . length - 1 ;
69
+ let left = 0 ;
70
+ let right = s . length - 1 ;
71
+ let skipLeft , skipRight , endsEqual = false ;
72
+
73
+ while ( left < right ) {
74
+ skipLeft = ! isAlphaNumeric ( s . charAt ( left ) )
75
+ if ( skipLeft ) { left ++ ; continue ; }
71
76
72
- while ( i < j ) {
73
- if ( ! isAlpha ( s . charAt ( i ) ) ) {
74
- i ++
75
- } else if ( ! isAlpha ( s . charAt ( j ) ) ) {
76
- j --
77
- } else if ( s . charAt ( i ) . toLowerCase ( ) !== s . charAt ( j ) . toLowerCase ( ) ) {
78
- return false
79
- } else {
80
- i ++
81
- j --
82
- }
77
+ skipRight = ! isAlphaNumeric ( s . charAt ( right ) )
78
+ if ( skipRight ) { right -- ; continue ; }
79
+
80
+ endsEqual = s . charAt ( left ) . toLowerCase ( ) === s . charAt ( right ) . toLowerCase ( )
81
+ if ( ! endsEqual ) return false
82
+
83
+ left ++
84
+ right --
83
85
}
84
86
return true
85
87
} ;
You can’t perform that action at this time.
0 commit comments