Reverse Words in a String
Input: "the sky is blue",
Output: "blue is sky the".Solution
public class Solution {
public String reverseWords(String s) {
String[] split = s.split(" ");
StringBuilder builder = new StringBuilder();
for (int i = split.length - 1; i >= 0; i--) {
if ("".equals(split[i])) continue;
builder.append(split[i]);
if (i != 0) {
builder.append(" ");
}
}
return builder.toString().trim();
}
}Last updated