File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -55,3 +55,33 @@ var isPalindrome = function(s) {
55
55
}
56
56
return true ;
57
57
} ;
58
+
59
+ /**
60
+ * 2 Pointer | Midde Convergence | No RegEx | No Copying
61
+ * Time O(N) | Space O(1)
62
+ * https://leetcode.com/problems/valid-palindrome/
63
+ * @param {string } s
64
+ * @return {boolean }
65
+ */
66
+ var isPalindrome = function ( s ) {
67
+ const isAlphaNumeric = c => ( c . toLowerCase ( ) >= 'a' && c . toLowerCase ( ) <= 'z' ) || c >= '0' && c <= '9'
68
+
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 ; }
76
+
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 --
85
+ }
86
+ return true
87
+ } ;
You can’t perform that action at this time.
0 commit comments