Logo
    Logo

    ©️ 2020-2026, Akhil Abraham.

    LinkedInGitHubMediumX
    Akhil Abraham
    Akhil Abraham
    /Posts
    Posts
    /LeetCode
    LeetCode
    /
    LeetCode
    /0557 - Reverse Words in a String III
    0557 - Reverse Words in a String III
    0557 - Reverse Words in a String III
    0557 - Reverse Words in a String III

    0557 - Reverse Words in a String III

    Difficulty
    Easy
    Language
    Python
    URL
    https://leetcode.com/problems/reverse-words-in-a-string-iii/

    Solution 1 - Split, Reverse, Join

    Split the string by spaces to get individual words. Reverse each word using slicing, then join them back together with spaces.

    TimeComplexity:O(n)TimeComplexity: O(n)TimeComplexity:O(n)
    SpaceComplexity:O(n)SpaceComplexity: O(n)SpaceComplexity:O(n)

    Solution 2 - Two Pointer In-Place Reversal

    Convert the string to a list of characters. Use two pointers to find each word boundary, then reverse the characters in each word in place. Finally, join the list back into a string.

    TimeComplexity:O(n)TimeComplexity: O(n)TimeComplexity:O(n)
    SpaceComplexity:O(n)SpaceComplexity: O(n)SpaceComplexity:O(n)
    """ 0557.1 - Reverse Words in a String III - Solution 1 - Split, Reverse, Join """
    
    #####################################################################################
    # Imports
    #####################################################################################
    
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def reverseWords(self, s: str) -> str:
            """Reverse Words in a String III Function"""
            return ' '.join([word[::-1] for word in s.split(' ')])
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().reverseWords("Let's take LeetCode contest"))  # "s'teL ekat edoCteeL tsetnoc"
        print(Solution().reverseWords("Mr Ding"))                       # "rM gniD"
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()
    
    """ 0557.2 - Reverse Words in a String III - Solution 2 - Two Pointer In-Place Reversal """
    
    #####################################################################################
    # Imports
    #####################################################################################
    
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def reverseWords(self, s: str) -> str:
            """Reverse Words in a String III Function"""
            chars = list(s)
            start = 0
    
            for end in range(len(chars) + 1):
                if end == len(chars) or chars[end] == ' ':
                    left, right = start, end - 1
                    while left < right:
                        chars[left], chars[right] = chars[right], chars[left]
                        left += 1
                        right -= 1
                    start = end + 1
    
            return ''.join(chars)
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().reverseWords("Let's take LeetCode contest"))  # "s'teL ekat edoCteeL tsetnoc"
        print(Solution().reverseWords("Mr Ding"))                       # "rM gniD"
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()