-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReverseWordsInString.java
47 lines (41 loc) · 1.3 KB
/
ReverseWordsInString.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
package strings.easy;
/***
* Problem 557 in Leetcode: https://leetcode.com/problems/reverse-words-in-a-string-iii/
*
* Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
*
* Example 1:
* Input: s = "Let's take LeetCode contest"
* Output: "s'teL ekat edoCteeL tsetnoc"
*
* Example 2:
* Input: s = "God Ding"
* Output: "doG gniD"
*/
public class ReverseWordsInString {
public static void main(String[] args) {
String s = "Let's take LeetCode contest";
System.out.println("Reversed string is: " + reverseWordsInString(s));
}
private static String reverseWordsInString(String s) {
int n = s.length();
char[] chars = s.toCharArray();
int start = 0, spaceFinder = 0;
while (start < n) {
while ((spaceFinder < n) && (chars[spaceFinder] != ' ')) {
spaceFinder++;
}
int end = spaceFinder - 1;
while (start < end) {
char temp = chars[start];
chars[start] = chars[end];
chars[end] = temp;
start++;
end--;
}
start = spaceFinder + 1;
spaceFinder++;
}
return new String(chars);
}
}