-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseWordsInAString.java
More file actions
149 lines (124 loc) · 4.07 KB
/
Copy pathReverseWordsInAString.java
File metadata and controls
149 lines (124 loc) · 4.07 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Source : https://leetcode.com/problems/reverse-words-in-a-string/
// Author : cornprincess
// Date : 2020-04-10
/*****************************************************************************************************
*
* Given an input string, reverse the string word by word.
*
* Example 1:
*
* Input: "the sky is blue"
* Output: "blue is sky the"
*
* Example 2:
*
* Input: " hello world! "
* Output: "world! hello"
* Explanation: Your reversed string should not contain leading or trailing spaces.
*
* Example 3:
*
* Input: "a good example"
* Output: "example good a"
* Explanation: You need to reduce multiple spaces between two words to a single space in the reversed
* string.
*
* Note:
*
* A word is defined as a sequence of non-space characters.
* Input string may contain leading or trailing spaces. However, your reversed string should
* not contain leading or trailing spaces.
* You need to reduce multiple spaces between two words to a single space in the reversed
* string.
*
* Follow up:
*
* For C programmers, try to solve it in-place in O(1) extra space.
******************************************************************************************************/
package ReverseWordsInAString;
import java.util.*;
public class ReverseWordsInAString {
// use jdk library
// Time Complexity O(n)
// Space Complexity O(n)
public String useLibrary(String s) {
List<String> list = Arrays.asList(s.trim().split("\\s+"));
Collections.reverse(list);
return String.join(" ", list);
}
// use own library
// Time Complexity O(n)
// Space Complexity O(n)
public String useOwnLibrary(String s) {
// trim space
StringBuilder stringBuilder = trimSpaces(s);
// reverse string
reverse(stringBuilder, 0, stringBuilder.length() - 1);
// reverse words
reverseEachWord(stringBuilder);
return stringBuilder.toString();
}
private StringBuilder trimSpaces(String s) {
int left = 0;
int right = s.length() - 1;
// trim left space
while (left <= right && s.charAt(left) == ' ') left++;
// trim right space
while (left <= right && s.charAt(right) == ' ') right--;
// remove the middle extra space
StringBuilder stringBuilder = new StringBuilder();
while (left <= right) {
char c = s.charAt(left);
if (c != ' ') {
stringBuilder.append(c);
} else if (stringBuilder.charAt(stringBuilder.length() - 1) != ' ') {
stringBuilder.append(c);
}
left++;
}
return stringBuilder;
}
// 1 2 3 4
// 1 2 3 4 5
private void reverse(StringBuilder sb, int left, int right) {
while (left < right) {
char temp = sb.charAt(left);
sb.setCharAt(left++, sb.charAt(right));
sb.setCharAt(right--, temp);
}
}
private void reverseEachWord(StringBuilder sb) {
int n = sb.length();
int start = 0;
int end = 0;
while (start < n) {
while (end < n && sb.charAt(end) != ' ') end++;
reverse(sb, start, end - 1);
start = end + 1;
end++;
}
}
public String deque(String s) {
int left = 0;
int right = s.length() -1;
// trim left and right space
while (left <= right && s.charAt(left) == ' ') left++;
while (left <= right && s.charAt(right) == ' ') right--;
Deque<String> d = new ArrayDeque<>();
StringBuilder word = new StringBuilder();
while (left <= right) {
char c = s.charAt(left);
if (word.length() != 0 && c == ' ') {
// add word into deque
d.offerFirst(word.toString());
word.setLength(0);
} else if (c != ' ') {
word.append(c);
}
left++;
}
// add the last word into deque
d.offerFirst(word.toString());
return String.join(" ", d);
}
}