-
Notifications
You must be signed in to change notification settings - Fork 5
/
ReverseWordsInAStringIII.java
43 lines (40 loc) · 1.3 KB
/
ReverseWordsInAStringIII.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
/*
Problem Statement:
Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
Problem Link:
Reverse Words In A String III: https://leetcode.com/problems/reverse-words-in-a-string-iii/
Solution:
https://github.com/sunnypatel165/leetcode-again/blob/master/solutions/ReverseWordsInAStringIII.java
Author:
Sunny Patel
https://github.com/sunnypatel165
https://www.linkedin.com/in/sunnypatel165/
*/
class Solution {
public String reverseWords(String s) {
char[] c = s.toCharArray();
for(int i =0;i<c.length;){
int space = getNextSpace(c, i);
reverseInRange(c, i, space-1);
i = space+1;
}
return new String(c);
}
public int getNextSpace(char s[], int start){
while(start<s.length){
if(s[start]==' ')
return start;
start++;
}
return start;
}
public void reverseInRange(char[] s, int start, int end){
int size = end-start+1;
for(int i=start;i<=(start + (end-start)/2);i++){
char c = s[i];
s[i]=s[start+size+start-i-1];
s[start+size+start-i-1]=c;
}
}
}