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 1+ function isPalindrome ( s : string ) : boolean {
2+ // 1. filter out non-alphanumeric characters
3+ const validChars =
4+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnlopqrstuvwxyz0123456789" ;
5+
6+ // 2. compare the first and last characters
7+ let i = 0 ;
8+ let j = s . length - 1 ;
9+
10+ while ( i < j ) {
11+ const head = s [ i ] ;
12+ const tail = s [ j ] ;
13+
14+ if ( ! validChars . includes ( head ) ) {
15+ // 3. if the characters are not alphanumeric, move the pointer inward
16+ i ++ ;
17+ } else if ( ! validChars . includes ( tail ) ) {
18+ // 3. if the characters are not alphanumeric, move the pointer inward
19+ j -- ;
20+ } else if ( head . toLowerCase ( ) !== tail . toLowerCase ( ) ) {
21+ // 4. if the characters are not the same, return false
22+ return false ;
23+ } else {
24+ // 5. if the characters are the same, move the pointers inward
25+ i ++ ;
26+ j -- ;
27+ }
28+ }
29+ return true ;
30+ }
You can’t perform that action at this time.
0 commit comments