-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathreverse_words_in_string.java
97 lines (79 loc) · 3.11 KB
/
reverse_words_in_string.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* Given an input string s, reverse the order of the words.
*
* A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space.
*
* Return a string of the words in reverse order concatenated by a single space.
*
* Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.
*
*
*
* Example 1:
*
* Input: s = "the sky is blue"
* Output: "blue is sky the"
* Example 2:
*
* Input: s = " hello world "
* Output: "world hello"
* Explanation: Your reversed string should not contain leading or trailing spaces.
*/
package Strings;
public class ReverseWordsInString {
public static void main(String[] args) {
String string = "the sky is blue";
String res = solve(string);
System.out.println(res);
}
public static String solve(String string) {
// O(N) time | O(N) - where N is the length of the array.
//Convert String to String Builder
//and trim spaces at the same time
StringBuilder stringBuilder = trimSpaces(string);
System.out.println(string);
//reverse the whole word
reverse(stringBuilder, 0, stringBuilder.length() - 1);
System.out.println(stringBuilder);
//reverse each word
reverseEachWord(stringBuilder);
System.out.println(stringBuilder);
return stringBuilder.toString();
}
public static StringBuilder trimSpaces(String string) {
int leftIdx = 0, rightIdx = string.length() - 1;
// remove leading spaces
while (leftIdx <= rightIdx && string.charAt(leftIdx) == ' ') ++leftIdx;
// remove trailing spaces
while (leftIdx <= rightIdx && string.charAt(rightIdx) == ' ') --rightIdx;
// reduce multiple spaces to single one
StringBuilder stringBuilder = new StringBuilder();
while (leftIdx <= rightIdx) {
char currentChar = string.charAt(leftIdx);
if (currentChar != ' ') stringBuilder.append(currentChar);
else if (stringBuilder.charAt(stringBuilder.length() - 1) != ' ') stringBuilder.append(currentChar);
++leftIdx;
}
return stringBuilder;
}
public static void reverse(StringBuilder stringBuilder, int leftIdx, int rightIdx) {
while (leftIdx < rightIdx) {
char temp = stringBuilder.charAt(leftIdx);
stringBuilder.setCharAt(leftIdx++, stringBuilder.charAt(rightIdx));
stringBuilder.setCharAt(rightIdx--, temp);
}
}
public static void reverseEachWord(StringBuilder stringBuilder) {
int len = stringBuilder.length();
int startIdx = 0, endIdx = 0;
while (startIdx < len) {
// go to the end of the word
while (endIdx < len && stringBuilder.charAt(endIdx) != ' ') ++endIdx;
// reverse the word
reverse(stringBuilder, startIdx, endIdx - 1);
// move to the next word
startIdx = endIdx + 1;
++endIdx;
}
}
}