• ㊗️
• 大家
• offer
• 多多！

## Problem

Given an input string, reverse the string word by word.

Example:

Input: "the sky is blue",
Output: "blue is sky the".


Note:

A word is defined as a sequence of non-space characters.
Input string may contain leading or trailing spaces. However, your reversed string should not contain leading or trailing spaces.
You need to reduce multiple spaces between two words to a single space in the reversed string.


Follow up: For C programmers, try to solve it in-place in O(1) space.

## Code

class Solution {
public String reverseWords(String s) {
StringBuilder sb = new StringBuilder();
String[] words = s.trim().split("\\s+");

for(int i = words.length - 1; i >= 0; i--){
sb.append(words[i] + " ");
}

return sb.toString().trim();
}
}


class Solution {
public String reverseWords(String s) {
StringBuilder sb = trimSpaces(s);

// reverse the whole string
reverse(sb, 0, sb.length() - 1);

// reverse each word
reverseEachWord(sb);

return sb.toString();
}

public StringBuilder trimSpaces(String s) {
int left = 0;
int right = s.length() - 1;

while (left <= right && s.charAt(left) == ' ') left++;
while (left <= right && s.charAt(right) == ' ') right--;

StringBuilder sb = new StringBuilder();
while (left <= right) {
char c = s.charAt(left);

if (c != ' ') sb.append(c);
else if (sb.charAt(sb.length() - 1) != ' ') sb.append(c);

left++;
}

return sb;
}

public void reverse(StringBuilder sb, int left, int right) {
while (left < right) {
char tmp = sb.charAt(left);
sb.setCharAt(left++, sb.charAt(right));
sb.setCharAt(right--, tmp);
}
}

public void reverseEachWord(StringBuilder sb) {
int n = sb.length();
int start = 0, end = 0;

while (start < n) {
while (end < n && sb.charAt(end) != ' ') ++end;
reverse(sb, start, end - 1);
start = end + 1;
end++;
}
}
}