557. 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:

1
2
Input: "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class  {
public String reverseWords(String s) {
if (s == null || s.length() == 0) return s;
String[] strs = s.split(" ");
StringBuilder sb = new StringBuilder();
for (int j = 0; j < strs.length; j++) {
String str = strs[j];
String st = "";
for (int i = str.length() - 1; i >= 0; i--) {
st += str.charAt(i);
}
strs[j] = st;
if (j != strs.length - 1) {

sb.append(strs[j] + " ");
} else {
sb.append(strs[j]);
}
}
return sb.toString();
}
}