java.lang.Object
g0101_0200.s0151_reverse_words_in_a_string.Solution

public class Solution extends Object
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 = &ldquo;the sky is blue&rdquo;</p> <p><strong>Output:</strong> &ldquo;blue is sky the&rdquo;</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &quot; hello world &quot;</p> <p><strong>Output:</strong> &ldquo;world hello&rdquo;</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 = &ldquo;a good example&rdquo;</p> <p><strong>Output:</strong> &ldquo;example good a&rdquo;</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 = &quot; Bob Loves Alice &quot;</p> <p><strong>Output:</strong> &ldquo;Alice Loves Bob&rdquo;</p> <p><strong>Example 5:</strong></p> <p><strong>Input:</strong> s = &ldquo;Alice does not even like bob&rdquo;</p> <p><strong>Output:</strong> &ldquo;bob like even not does Alice&rdquo;</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 Details

    • Solution

      public Solution()
  • Method Details