File tree 2 files changed +46
-0
lines changed
solution/0917.Reverse Only Letters
2 files changed +46
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ string reverseOnlyLetters (string S) {
4
+
5
+ int start = 0 , end = S.length ()-1 ;
6
+ char temp;
7
+
8
+ while ( start < end ){
9
+
10
+ while ( start < end && ! isalpha (S[start]) )
11
+ start++;
12
+ while ( start < end && ! isalpha (S[end]) )
13
+ end--;
14
+
15
+ swap (S[start], S[end]);
16
+ start++;
17
+ end--;
18
+ }
19
+
20
+ return S;
21
+ }
22
+ };
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def reverseOnlyLetters (self , S ):
3
+ """
4
+ :type S: str
5
+ :rtype: str
6
+ """
7
+
8
+ S = list (S )
9
+
10
+ start = 0
11
+ end = len (S )- 1
12
+
13
+ while start < end :
14
+
15
+ while start < end and S [start ] not in string .ascii_letters :
16
+ start += 1
17
+ while start < end and S [end ] not in string .ascii_letters :
18
+ end -= 1
19
+
20
+ S [start ], S [end ] = S [end ], S [start ]
21
+ start += 1
22
+ end -= 1
23
+
24
+ return '' .join (S )
You can’t perform that action at this time.
0 commit comments