File tree 2 files changed +5
-5
lines changed
src/0917-Reverse-Only-Letters
2 files changed +5
-5
lines changed Original file line number Diff line number Diff line change 1
1
#include < iostream>
2
2
#include < vector>
3
3
#include < string>
4
+ #include < cctype>
4
5
using namespace std ;
5
6
6
7
static int x = []() {std::ios::sync_with_stdio (false ); cin.tie (0 ); return 0 ; }();
@@ -12,12 +13,12 @@ class Solution
12
13
int l = 0 , r = S.size () - 1 ;
13
14
while (l < r)
14
15
{
15
- if (!( ' a ' <= S[l] and S[l] <= ' z ' ) and !( ' A ' <= S[l] and S[l] <= ' Z ' ))
16
+ if (!isalpha ( S[l]))
16
17
{
17
18
++l;
18
19
continue ;
19
20
}
20
- if (!( ' a ' <= S[r] and S[r] <= ' z ' ) and !( ' A ' <= S[r] and S[r] <= ' Z ' ))
21
+ if (!isalpha ( S[r]))
21
22
{
22
23
--r;
23
24
continue ;
Original file line number Diff line number Diff line change 1
- import string
2
1
class Solution :
3
2
def reverseOnlyLetters (self , S ):
4
3
"""
5
4
:type S: str
6
5
:rtype: str
7
6
"""
8
- p = [i for i in S if i in string . ascii_letters ]
9
- return '' .join ([i if i not in string . ascii_letters else p .pop () for i in S ])
7
+ p = [i for i in S if i . isalpha () ]
8
+ return '' .join ([i if not i . isalpha () else p .pop () for i in S ])
10
9
11
10
if __name__ == "__main__" :
12
11
S = "a-bC-dEf-ghIj"
You can’t perform that action at this time.
0 commit comments