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