// Source : https://leetcode.com/problems/reverse-words-in-a-string/description/
// Author : Tianming Cao
// Date : 2018-02-11
package reverseWordsInAString;
public class ReverseWordsInAString {
public String reverseWords(String s) {
if (s == null || s.length() == 0) {
return s;
}
s = s.trim();
String[] array = s.split("\\s+");
StringBuilder sb = new StringBuilder();
int len = array.length;
for (int i = len - 1; i > 0; i--) {
sb.append(array[i]).append(" ");
}
sb.append(array[0]);
return sb.toString();
}
}