File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed
Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param {string } s
3+ * @return {boolean }
4+ */
5+ var isPalindrome = function ( s ) {
6+ const stack = [ ] ;
7+ for ( let i = 0 ; i < s . length ; i ++ ) {
8+ const val = s [ i ]
9+ if ( ( val . charCodeAt ( ) >= 'A' . charCodeAt ( ) && val . charCodeAt ( ) <= 'Z' . charCodeAt ( ) ) ) {
10+ stack . push ( String . fromCharCode ( val . charCodeAt ( ) + 'a' . charCodeAt ( ) - 'A' . charCodeAt ( ) ) )
11+ } else if ( ( val . charCodeAt ( ) >= 'a' . charCodeAt ( ) && val . charCodeAt ( ) <= 'z' . charCodeAt ( ) ) ) {
12+ stack . push ( val )
13+ } else if ( val . charCodeAt ( ) >= '0' . charCodeAt ( ) && val . charCodeAt ( ) <= '9' . charCodeAt ( ) ) {
14+ stack . push ( val )
15+ }
16+ }
17+ if ( stack . length % 2 === 0 ) {
18+ let start = ( stack . length / 2 ) - 1
19+ let end = stack . length / 2
20+ let flag = true
21+ while ( true ) {
22+ if ( stack [ start ] !== stack [ end ] ) {
23+ flag = false
24+ break ;
25+ }
26+ start --
27+ end ++
28+ if ( start < 0 || end > stack . length ) break
29+ }
30+ return flag
31+ } else {
32+ let start = Math . floor ( stack . length / 2 )
33+ let end = Math . floor ( stack . length / 2 )
34+ let flag = true
35+ while ( true ) {
36+ if ( stack [ start ] !== stack [ end ] ) {
37+ flag = false
38+ break ;
39+ }
40+ start --
41+ end ++
42+ if ( start < 0 || end > stack . length ) break
43+ }
44+ return flag
45+ }
46+
47+
48+
49+ } ;
You can’t perform that action at this time.
0 commit comments