Skip to content

Commit bf88a9a

Browse files
refactor 917
1 parent f104fcc commit bf88a9a

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

src/0917-Reverse-Only-Letters/0917.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <iostream>
22
#include <vector>
33
#include <string>
4+
#include <cctype>
45
using namespace std;
56

67
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
@@ -12,12 +13,12 @@ class Solution
1213
int l = 0, r = S.size() - 1;
1314
while (l < r)
1415
{
15-
if (!('a' <= S[l] and S[l] <= 'z') and !('A' <= S[l] and S[l] <= 'Z'))
16+
if (!isalpha(S[l]))
1617
{
1718
++l;
1819
continue;
1920
}
20-
if (!('a' <= S[r] and S[r] <= 'z') and !('A' <= S[r] and S[r] <= 'Z'))
21+
if (!isalpha(S[r]))
2122
{
2223
--r;
2324
continue;

src/0917-Reverse-Only-Letters/0917.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import string
21
class Solution:
32
def reverseOnlyLetters(self, S):
43
"""
54
:type S: str
65
:rtype: str
76
"""
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])
109

1110
if __name__ == "__main__":
1211
S = "a-bC-dEf-ghIj"

0 commit comments

Comments
 (0)