-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDay 16.1.txt
48 lines (36 loc) · 1013 Bytes
/
Day 16.1.txt
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
557. 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"
Constraints:
1 <= s.length <= 5 * 104
s contains printable ASCII characters.
s does not contain any leading or trailing spaces.
There is at least one word in s.
All the words in s are separated by a single space.
class Solution {
public String reverseWords(String s) {
s=s.trim();
int i;
char c;
String p="",q="";
for(i=0;i<s.length();i++)
{
c=s.charAt(i);
if(c==' ')
{
p=p+q+" ";
q="";
}
else
{
q=s.charAt(i)+q;
}
}
return p+q;
}
}