➜ ~

Playing Hacks and Stuffs!


Project maintained by h4ckyou Hosted on GitHub Pages — Theme by mattgraham

Reverse Only Letters

image

Given a string s, reverse the string according to the following rules:

Return s after reversing it.

My approach is this:

Solve Script: link image

Leetcode Submission Script

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