class Solution {
public String reverseWords(String s) {
int n = s.length();
StringBuilder ans = new StringBuilder(n);
for (int i = n - 1; i >= 0; i--) {
int j = i;
StringBuilder x = new StringBuilder();
while (j >= 0 && s.charAt(j) != ' ') {
x.append(s.charAt(j--));
}
ans.append(x.reverse().toString());
i = j;
if (i > 0) {
ans.append(" ");
}
}
return ans.toString();
}
}