Description

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"

Constraints:

  • 1 <= s.length <= 5 * 10^4
  • s contains printable ASCII characters.
  • s does not contain any leading or trailing spaces.
  • There is at least one word in s.
  • All the words in s are separated by a single space.

Solution

The solution for this problem might be to split the word by space and then use two pointers approach to reverse each of those strings. This would also work in O(n) time complexity. Again, we could use reverse method to reverse a string, but we will iterate through elements twice in this case.

 1class Solution {
 2    public String reverseWords(String s) {
 3        StringBuilder builder = new StringBuilder();
 4        for (String word: s.split(" ")) {
 5            builder.append(new StringBuffer(word).reverse().toString());
 6            builder.append(" ");
 7        }
 8        return builder.substring(0, builder.length() - 1);
 9    }
10}
 1class Solution {
 2    public String reverseWords(String s) {
 3        StringBuilder builder = new StringBuilder();
 4        for (String word: s.split(" ")) {
 5            for (int i = word.length() - 1; i >= 0; i--) {
 6                builder.append(word.charAt(i));
 7            }
 8            builder.append(" ");
 9        }
10        return builder.substring(0, builder.length() - 1);
11    }
12}