Skip to content

Commit

Permalink
Reverse Only Letters
Browse files Browse the repository at this point in the history
  • Loading branch information
rinwf committed Oct 9, 2018
1 parent 34ec304 commit 26d8bbd
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lc917. Reverse Only Letters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution:
def reverseOnlyLetters(self, S):
"""
:type S: str
:rtype: str
"""
s2 = list(S)
i = 0
j = len(S) - 1
while i < j:
if S[i].isalpha():
if S[j].isalpha():
s2[i], s2[j] = s2[j], s2[i]
i += 1
j -= 1
else:
j -= 1
else:
i += 1
return ''.join(s2)

0 comments on commit 26d8bbd

Please sign in to comment.