forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_151.java
23 lines (21 loc) · 796 Bytes
/
_151.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.fishercoder.solutions;
/**Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue",
return "blue is sky the".*/
public class _151 {
public String reverseWords(String s) {
if(!s.contains(" ")) return s;//for cases like this: "a"
if(s.matches(" *")) return "";//for cases like this: " "
String[] words = s.split(" ");
StringBuilder stringBuilder = new StringBuilder();
for(int i = words.length-1; i >= 0; i--){
if(!words[i].equals("") && !words[i].equals(" ")){
stringBuilder.append(words[i]);
stringBuilder.append(" ");
}
}
stringBuilder.deleteCharAt(stringBuilder.length()-1);
return stringBuilder.toString();
}
}