Playing Hacks and Stuffs!
Given a string s, reverse the string according to the following rules:
Return s after reversing it.
My approach is this:
left and right which would hold the 0th and last index len(s)-1 values[left] of the string is not alpha() I’ll make a while loop that checks for that and I’ll increment the left pointer by 1s[right] of the string is not alpha() I’ll make a while loop that checks for that and I’ll decrement the right pointer by 1s[left] and s[right] is alpha() I’ll then swap them i.e I’ll set s[left], s[right] = s[right], s[left]Solve Script: link
class Solution:
def reverseOnlyLetters(self, s: str) -> str:
s = list(s)
left, right = 0, len(s)-1
while left < right:
while left < right and s[left].isalpha() == False:
left += 1
while left < right and s[right].isalpha() == False:
right -= 1
if s[left].isalpha() and s[right].isalpha():
s[left], s[right] = s[right], s[left]
left += 1
right -= 1
r = "".join(s)
return r