Isaac Emmanuel
15 Jun 2024•EasyC++ / Python / TypeScript

Reverse Words in a String III

C++, Python and TypeScript solutions for Reverse Words in a String III.

The Reverse Words in a String III solutions are below.

Solution files

Python•reverse-words-in-a-string-iii/synced-solution.py
class Solution:
    def reverseWords(self, s: str) -> str:
        return " ".join(word[::-1] for word in s.split(" "))
C++•reverse-words-in-a-string-iii/synced-solution.cpp
class Solution {
public:
    string reverseWords(string s) {
        stringstream stream(s);
        string word;
        string result;

        while (stream >> word) {
            reverse(word.begin(), word.end());
            if (!result.empty()) {
                result += ' ';
            }
            result += word;
        }
        return result;
    }
};
TypeScript•reverse-words-in-a-string-iii/synced-solution.ts
function reverseWords(s: string): string {
    return s
        .split(" ")
        .map(word => word.split("").reverse().join(""))
        .join(" ");
}