Skip to content

Commit 47054c1

Browse files
committed
Sync LeetCode submission - Reverse Words in a String - Runtime - 0 ms (100.00%), Memory - 17.3 MB (95.12%)
1 parent ad7e24c commit 47054c1

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
2+
3+
<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>
4+
5+
<p>Return <em>a string of the words in reverse order concatenated by a single space.</em></p>
6+
7+
<p><b>Note</b> 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>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
12+
<pre>
13+
<strong>Input:</strong> s = &quot;the sky is blue&quot;
14+
<strong>Output:</strong> &quot;blue is sky the&quot;
15+
</pre>
16+
17+
<p><strong class="example">Example 2:</strong></p>
18+
19+
<pre>
20+
<strong>Input:</strong> s = &quot; hello world &quot;
21+
<strong>Output:</strong> &quot;world hello&quot;
22+
<strong>Explanation:</strong> Your reversed string should not contain leading or trailing spaces.
23+
</pre>
24+
25+
<p><strong class="example">Example 3:</strong></p>
26+
27+
<pre>
28+
<strong>Input:</strong> s = &quot;a good example&quot;
29+
<strong>Output:</strong> &quot;example good a&quot;
30+
<strong>Explanation:</strong> You need to reduce multiple spaces between two words to a single space in the reversed string.
31+
</pre>
32+
33+
<p>&nbsp;</p>
34+
<p><strong>Constraints:</strong></p>
35+
36+
<ul>
37+
<li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li>
38+
<li><code>s</code> contains English letters (upper-case and lower-case), digits, and spaces <code>&#39; &#39;</code>.</li>
39+
<li>There is <strong>at least one</strong> word in <code>s</code>.</li>
40+
</ul>
41+
42+
<p>&nbsp;</p>
43+
<p><b data-stringify-type="bold">Follow-up:&nbsp;</b>If the string data type is mutable in your language, can&nbsp;you solve it&nbsp;<b data-stringify-type="bold">in-place</b>&nbsp;with&nbsp;<code data-stringify-type="code">O(1)</code>&nbsp;extra space?</p>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def reverseWords(self, s: str) -> str:
3+
splited_str = s.split()
4+
reversed_lst = splited_str[::-1]
5+
return ' '.join(reversed_lst)
6+

0 commit comments

Comments
 (0)