Blogs Hub

Reverse Words in a String III - MiniTV

Reverse Words in a String III - मिनी टीवी

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

Example 1:

Input: "Let's take LeetCode contest"

Output: "s'teL ekat edoCteeL tsetnoc"


Note: In the string, each word is separated by a single space and there will not be any extra space in the string.



using System;
using System.Collections.Generic;
using System.Text;

namespace LeetCode.AskGif.Easy.String
    public class ReverseWordsSoln
        public string ReverseWords(string s)
            var strArr = s.Split(" ");
            var str = new StringBuilder();
            foreach (var item in strArr)
                for (int i = item.Length-1; i >= 0; i--)
                str.Append(" ");
            return str.ToString().Trim();


Time Complexity: O(n)

Space Complexity: O(n)