Skip to content

Commit 5a3da63

Browse files
committed
Reverse words in a string
1 parent 8ce69b5 commit 5a3da63

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package popular;
2+
3+
public class ReverseWordsInAString {
4+
5+
public String reverseWords(String s) {
6+
s = s.trim();
7+
if (s.length() == 0) {
8+
return s;
9+
}
10+
String blankString = " ";
11+
String[] wordsArray = s.split(blankString);
12+
StringBuilder sb = new StringBuilder();
13+
14+
for (int i = wordsArray.length - 1; i >= 0; i--) {
15+
String word = wordsArray[i];
16+
word = word.trim();
17+
if (word.length() > 0) {
18+
sb.append(word).append(blankString);
19+
}
20+
}
21+
22+
return sb.toString().trim();
23+
}
24+
25+
public String reverseWordsWithLessSpace(String s) {
26+
char[] chars = s.toCharArray();
27+
int len = chars.length;
28+
subReverse(chars, 0, len - 1);
29+
subReverseWords(chars, len);
30+
31+
return subCleanBlankChar(chars, len);
32+
}
33+
34+
public String subCleanBlankChar(char[] chars, int len) {
35+
int i = 0, j = 0;
36+
char blankChar = ' ';
37+
while (j < len) {
38+
while (j < len && chars[j] == blankChar) {
39+
j++;
40+
}
41+
while (j < len && chars[j] != blankChar) {
42+
chars[i++] = chars[j++];
43+
}
44+
while (j < len && chars[j] == blankChar) {
45+
j++;
46+
}
47+
if (j < len) {
48+
chars[i++] = blankChar;
49+
}
50+
}
51+
52+
return new String(chars).substring(0, i);
53+
}
54+
55+
public void subReverseWords(char[] chars, int len) {
56+
char blankChar = ' ';
57+
int i = 0, j = 0;
58+
while (j < len) {
59+
while (j < len && chars[j] == blankChar) {
60+
j++;
61+
}
62+
i = j;
63+
while (j < len && chars[j] != blankChar) {
64+
j++;
65+
}
66+
subReverse(chars, i, j - 1);
67+
}
68+
}
69+
70+
public void subReverse(char[] chars, int startIndex, int endIndex) {
71+
while (startIndex < endIndex) {
72+
char temp = chars[startIndex];
73+
chars[startIndex] = chars[endIndex];
74+
chars[endIndex] = temp;
75+
76+
startIndex++;
77+
endIndex--;
78+
}
79+
}
80+
81+
public static void main(String[] args) {
82+
String input = "the sky is blue";
83+
ReverseWordsInAString obj = new ReverseWordsInAString();
84+
//System.out.println("Input: \"" + input + "\"\n" + "Output: \"" + obj.reverseWords(input) + "\"") ;
85+
System.out.println("Input: \"" + input + "\"\n" + "Output: \"" + obj.reverseWordsWithLessSpace(input) + "\"") ;
86+
87+
String input1 = " hello world! ";
88+
//System.out.println("Input: \"" + input1 + "\"\n" + "Output: \"" + obj.reverseWords(input1) + "\"") ;
89+
System.out.println("Input: \"" + input1 + "\"\n" + "Output: \"" + obj.reverseWordsWithLessSpace(input1) + "\"") ;
90+
91+
String input2 = "a good example";
92+
//System.out.println("Input: \"" + input2 + "\"\n" + "Output: \"" + obj.reverseWords(input2) + "\"") ;
93+
System.out.println("Input: \"" + input2 + "\"\n" + "Output: \"" + obj.reverseWordsWithLessSpace(input2) + "\"") ;
94+
}
95+
}

0 commit comments

Comments
 (0)