Reverse Words in a String III LeetCode Solution

Difficulty Level Easy
Frequently asked in Amazon Apple Bloomberg eBay Facebook Google Microsoft PayPal Paytm Snapchat Yandex
Bolt ZapposViews 5295

Problem Statement

Reverse Words in a String III LeetCode Solution – We are given a string and are asked to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

Examples & Explanations

Example 1:

Input: s = "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"
Exaplanation: Each word is reversed in the ouput while preserving whitespaces

Example 2:

Input: s = "God Ding"
Output: "doG gniD"
Explanation: reverse of God is doG & Ding is gniD

Approach

This problem can be used using simple implementation. We will use a loop to iterate over all the characters one by one. Let’s use a temporary string t to store the characters of a word. Keep appending or pushing back characters in t till we hit whitespace or reach the end of the given sentence. Reverse the characters in the string t and push it into the answer string “res” and empty string t.

The approach used in Java implementation differs because strings are a little difficult to handle as they are immutable in Java. So, instead of using a string, we will convert it into a char array and find the first and last index of each word present in the sentence and reverse it.

* Traverse the given string.

* Store the each character till you not counter an space

* For simplicity i have pushed an empty space at last.

* When you counter a space pass the string to function for reversing.

* Store the return string in my temporary variable.

* Storing it in a resulting string.

* Pushing an empty space at last of resulting to seprate the word.

* But NOT to add space at last word. For that i have if statement if((i+1)<s.length())

Code

C++ code for Reverse Words in a String III

class Solution {
public:
    string reverseWords(string s) {
        string res="", t="";
        int n = s.size();
        for(int i=0; i<=n; i++) {
            if(i == n){
                reverse(t.begin(), t.end());
                res += t;
            }
            else if(s[i] == ' ' || i==n) {
                reverse(t.begin(), t.end());
                res += t + " ";
                
                t="";
            }
            else {
                t+=s[i];
            }
        }
        return res;
    }
};

Java code for Reverse Words in a String III

class Solution {
    public String reverseWords(String s) {
        
        int len = s.length(); 
        if(len == 1) 
            return s;
        
        int firstIndex, lastIndex;
        char[] res = s.toCharArray(); 
        char temp;
        
        for(int index = 0 ; index < len ; index++){
            
            firstIndex = index; 
            
            while(++index < len && res[index] != ' '); 
            lastIndex = index - 1; 
            
      // reversing characters of the word
            while(firstIndex < lastIndex){
                temp = res[firstIndex];
                res[firstIndex++] = res[lastIndex];
                res[lastIndex--] = temp;
            }
        }
        
        return new String(res); 
    }
}

Complexity Analysis for Reverse Words in a String III LeetCode Solution

Translate »