➜ ~

Playing Hacks and Stuffs!


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

Reverse Words in a String III

image

Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

To solve this I just converted the words in the string to a list and then iterate through the list and reverse the characters, then returned it’s reformed value

Solve Script: link image

Leetcode Submission Script

class Solution:
    def reverseWords(self, s: str) -> str:
        arr = s.split()
        r = []

        for i in range(len(arr)):
            r.append(arr[i][::-1])

        return " ".join(r)