Class Solution
java.lang.Object
g0101_0200.s0151_reverse_words_in_a_string.Solution
151 - Reverse Words in a String.<p>Medium</p>
<p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order concatenated by a single space.</em></p>
<p><strong>Note</strong> that <code>s</code> may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> s = “the sky is blue”</p>
<p><strong>Output:</strong> “blue is sky the”</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> s = " hello world "</p>
<p><strong>Output:</strong> “world hello”</p>
<p><strong>Explanation:</strong> Your reversed string should not contain leading or trailing spaces.</p>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> s = “a good example”</p>
<p><strong>Output:</strong> “example good a”</p>
<p><strong>Explanation:</strong> You need to reduce multiple spaces between two words to a single space in the reversed string.</p>
<p><strong>Example 4:</strong></p>
<p><strong>Input:</strong> s = " Bob Loves Alice "</p>
<p><strong>Output:</strong> “Alice Loves Bob”</p>
<p><strong>Example 5:</strong></p>
<p><strong>Input:</strong> s = “Alice does not even like bob”</p>
<p><strong>Output:</strong> “bob like even not does Alice”</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> contains English letters (upper-case and lower-case), digits, and spaces <code>' '</code>.</li>
<li>There is <strong>at least one</strong> word in <code>s</code>.</li>
</ul>
<p><strong>Follow-up:</strong> If the string data type is mutable in your language, can you solve it <strong>in-place</strong> with <code>O(1)</code> extra space?</p>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
reverseWords
-