Skip to content

Commit d918fa3

Browse files
committed
Add solution #917
1 parent 538a568 commit d918fa3

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,400 LeetCode solutions in JavaScript
1+
# 1,401 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -733,6 +733,7 @@
733733
914|[X of a Kind in a Deck of Cards](./solutions/0914-x-of-a-kind-in-a-deck-of-cards.js)|Medium|
734734
915|[Partition Array into Disjoint Intervals](./solutions/0915-partition-array-into-disjoint-intervals.js)|Medium|
735735
916|[Word Subsets](./solutions/0916-word-subsets.js)|Medium|
736+
917|[Reverse Only Letters](./solutions/0917-reverse-only-letters.js)|Easy|
736737
918|[Maximum Sum Circular Subarray](./solutions/0918-maximum-sum-circular-subarray.js)|Medium|
737738
919|[Complete Binary Tree Inserter](./solutions/0919-complete-binary-tree-inserter.js)|Medium|
738739
920|[Number of Music Playlists](./solutions/0920-number-of-music-playlists.js)|Hard|
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* 917. Reverse Only Letters
3+
* https://leetcode.com/problems/reverse-only-letters/
4+
* Difficulty: Easy
5+
*
6+
* Given a string s, reverse the string according to the following rules:
7+
* - All the characters that are not English letters remain in the same position.
8+
* - All the English letters (lowercase or uppercase) should be reversed.
9+
*
10+
* Return s after reversing it.
11+
*/
12+
13+
/**
14+
* @param {string} s
15+
* @return {string}
16+
*/
17+
var reverseOnlyLetters = function(s) {
18+
const chars = s.split('');
19+
let left = 0;
20+
let right = s.length - 1;
21+
22+
while (left < right) {
23+
while (left < right && !/[a-zA-Z]/.test(chars[left])) {
24+
left++;
25+
}
26+
while (left < right && !/[a-zA-Z]/.test(chars[right])) {
27+
right--;
28+
}
29+
[chars[left], chars[right]] = [chars[right], chars[left]];
30+
left++;
31+
right--;
32+
}
33+
34+
return chars.join('');
35+
};

0 commit comments

Comments
 (0)