Skip to content

Commit e5915fe

Browse files
committed
917 - Python, C++
1 parent 6042353 commit e5915fe

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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)

0 commit comments

Comments
 (0)