less than 1 minute read

557. Reverse Words in a String III

Difficulty: Easy

Related Topics: Two Pointers, String

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

Example 1:

Input: s = “Let’s take LeetCode contest” Output: “s’teL ekat edoCteeL tsetnoc”

Example 2:

Input: s = “God Ding” Output: “doG gniD”

Code:

class Solution:
    def reverseWords(self, s: str) -> str:
        splited = s.split()
        newsplit = []
        for i in splited:
            i = i[::-1]
            newsplit.append(i)
        return " ".join(newsplit)

Feedbacks:

Used “.split” to split string by blankspace to words, and added reversed words to new list and return them using “join” function. But I don’t get how should I implement two pointers in this problem.