-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseWordsInString.java
More file actions
155 lines (120 loc) · 4.83 KB
/
ReverseWordsInString.java
File metadata and controls
155 lines (120 loc) · 4.83 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
150
151
152
153
154
155
package Algorithms.Strings;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 05 April 2025
* @link 151. Reverse Words in a String <a href="https://leetcode.com/problems/reverse-words-in-a-string/">LeetCode link</a>
* @topics String, Two Pointers
*/
public class ReverseWordsInString {
public static void main(String[] args) {
String s = "the sky is blue";
System.out.println("reverseWords(s) => " + reverseWords(s));
System.out.println("reverseWordsMyApproach(s) => " + reverseWordsMyApproach(s));
System.out.println("reverseWords2(s) => " + reverseWords2(s));
}
/**
* @TimeComplexity O(n)
* @SpaceComplexity O(n)
*/
public static String reverseWords(String s) {
s = s.trim();
String[] words = s.split("\\s+");
StringBuilder reversed = new StringBuilder();
for (int i = words.length - 1; i >= 0; i--) {
reversed.append(words[i]);
if (i != 0) reversed.append(" "); // add space between words except after the last word
}
return reversed.toString();
}
public static String reverseWordsMyApproach(String s) {
String[] arr = s.split(" ");
StringBuilder sb = new StringBuilder();
for(int i=arr.length-1; i>=0; i--) {
if(!arr[i].isBlank()) sb.append(arr[i]).append(" ");
}
return sb.toString().trim();
}
/**
* @TimeComplexity O(n)
* @SpaceComplexity O(w), where is maxWordLength
*/
public static String reverseWords2(String s) {
StringBuilder sb = new StringBuilder();
StringBuilder temp = new StringBuilder(); // or use new int[2]{start, end} and just use sb.append(s.substring(start, end))
s=s.trim();
for(int i=0; i<s.length(); i++) { // or for(char c: s.toCharArray()){
char c = s.charAt(i);
if(c != ' ') {
temp.append(c);
} else if(!temp.isEmpty()) {
sb.insert(0, " "+ temp);
temp.setLength(0);
}
}
return sb.insert(0, temp).toString(); // for the last word we don't see ' ' cause we did s = s.trim();
}
public static String reverseWordsUsingTwoPointers(String s) {
char[] ca = s.toCharArray();
int n = ca.length;
char[] result = new char[n];
int result_index = 0;
int end = n - 1;
while (end >= 0) {
while (end >= 0 && ca[end] == ' ') end--; // skip spaces
int start = end;
while (start >= 0 && ca[start] != ' ') start--; // frame word
if (result_index > 0) result[result_index++] = ' '; // add space between words
for (int i = start + 1; i <= end; i++) // put that word into result
result[result_index++] = ca[i];
end = start - 1;
}
return new String(result).trim(); // or new String(result, 0, result_index).trim();
}
public String reverseWordsUsingList(String s) {
List<String> list = Arrays.asList(s.trim().split("\\s+"));
Collections.reverse(list);
return String.join(" ", list); // String.join works for char[], String[], List<String> and individual strings
}
public String reverseWordsUsingStream1(String s) {
List<String> list = Arrays.stream(s.trim().split("\\s+")).collect(Collectors.toList());
Collections.reverse(list);
return list.stream().collect(Collectors.joining(" ")); // or return String.join(" ", list);
}
public String reverseWordsUsingStream2(String s) {
return String.join(" ", Arrays.stream(s.trim().split("\\s+"))
.collect(Collectors.collectingAndThen(
Collectors.toList(),
(List<String> list) -> { // use "(list) -> {" -- will throw error: reference to join is ambiguous. So use "(List<String> list) -> {"
Collections.reverse(list);
return list;
}
)));
}
public String reverseWordsUsingStream3(String s) {
return Arrays.stream(s.trim().split("\\s+")).collect(Collectors.collectingAndThen(
Collectors.toList(),
(list) -> {
Collections.reverse(list);
return list;
}
)).stream().collect(Collectors.joining(" "));
}
public static String reverseWords3(String s) {
StringBuilder sb = new StringBuilder();
int n = s.length();
for (int i = n - 1; i >= 0; i--) {
if (s.charAt(i) != ' ') {
while (i >= 0 && s.charAt(i) != ' ') {
sb.append(s.charAt(i));
i--;
}
sb.append(' ');
}
}
return sb.toString().trim();
}
}