Skip to content

Commit 2b0afbe

Browse files
authored
Create 917-reverse-only-letters.js
1 parent 8590e6a commit 2b0afbe

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

917-reverse-only-letters.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
}

0 commit comments

Comments
 (0)