-
Notifications
You must be signed in to change notification settings - Fork 160
/
reverse-words-in-a-string.md
42 lines (29 loc) · 1.34 KB
/
reverse-words-in-a-string.md
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
<p>Given an input string, reverse the string word by word.</p>
<p> </p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> "<code>the sky is blue</code>"
<strong>Output: </strong>"<code>blue is sky the</code>"
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong> " hello world! "
<strong>Output: </strong>"world! hello"
<strong>Explanation:</strong> Your reversed string should not contain leading or trailing spaces.
</pre>
<p><strong>Example 3:</strong></p>
<pre>
<strong>Input:</strong> "a good example"
<strong>Output: </strong>"example good a"
<strong>Explanation:</strong> You need to reduce multiple spaces between two words to a single space in the reversed string.
</pre>
<p> </p>
<p><strong>Note:</strong></p>
<ul>
<li>A word is defined as a sequence of non-space characters.</li>
<li>Input string may contain leading or trailing spaces. However, your reversed string should not contain leading or trailing spaces.</li>
<li>You need to reduce multiple spaces between two words to a single space in the reversed string.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong></p>
<p>For C programmers, try to solve it <em>in-place</em> in <em>O</em>(1) extra space.</p>