917. Reverse Only Letters
Approach 1: Front and Back switching
class Solution:
def reverseOnlyLetters(self, S):
"""
https://leetcode.com/problems/reverse-only-letters/description/
:type S: str
:rtype: str
"""
if S == "":
return ""
S = list(S)
i, j = 0, len(S) - 1
while i < j:
if S[i].isalpha() == False:
i += 1
if S[j].isalpha() == False:
j -= 1
else:
S[i], S[j] = S[j], S[i]
i += 1
j -= 1
return ''.join(S)
Approach 2: Minor improvements to Approach 1
Last updated