File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } S
3
+ * @return {string }
4
+ */
5
+ const reverseOnlyLetters = function ( S ) {
6
+ let start = 0
7
+ let end = S . length - 1
8
+ const arr = S . split ( "" )
9
+ while ( start < end ) {
10
+ while ( start < end && ! chk ( S . charCodeAt ( start ) ) ) {
11
+ start ++
12
+ }
13
+ while ( start < end && ! chk ( S . charCodeAt ( end ) ) ) {
14
+ end --
15
+ }
16
+
17
+ let tmp = S [ end ]
18
+ arr [ end ] = S [ start ]
19
+ arr [ start ] = tmp
20
+ start ++
21
+ end --
22
+ }
23
+ return arr . join ( "" )
24
+ }
25
+
26
+ function chk ( num ) {
27
+ const aCode = "a" . charCodeAt ( 0 )
28
+ const zCode = "z" . charCodeAt ( 0 )
29
+ const ACode = "A" . charCodeAt ( 0 )
30
+ const ZCode = "Z" . charCodeAt ( 0 )
31
+
32
+ if ( ( num >= aCode && num <= zCode ) || ( num >= ACode && num <= ZCode ) ) {
33
+ return true
34
+ } else {
35
+ return false
36
+ }
37
+ }
You can’t perform that action at this time.
0 commit comments