2023-12-09 18:42:21 +08:00
|
|
|
<p>Given a string <code>s</code> consisting of words and spaces, return <em>the length of the <strong>last</strong> word in the string.</em></p>
|
2022-03-27 18:35:17 +08:00
|
|
|
|
2023-12-09 18:42:21 +08:00
|
|
|
<p>A <strong>word</strong> is a maximal <span data-keyword="substring-nonempty">substring</span> consisting of non-space characters only.</p>
|
2022-03-27 18:35:17 +08:00
|
|
|
|
|
|
|
<p> </p>
|
2023-12-09 18:42:21 +08:00
|
|
|
<p><strong class="example">Example 1:</strong></p>
|
2022-03-27 18:35:17 +08:00
|
|
|
|
|
|
|
<pre>
|
|
|
|
<strong>Input:</strong> s = "Hello World"
|
|
|
|
<strong>Output:</strong> 5
|
|
|
|
<strong>Explanation:</strong> The last word is "World" with length 5.
|
|
|
|
</pre>
|
|
|
|
|
2023-12-09 18:42:21 +08:00
|
|
|
<p><strong class="example">Example 2:</strong></p>
|
2022-03-27 18:35:17 +08:00
|
|
|
|
|
|
|
<pre>
|
|
|
|
<strong>Input:</strong> s = " fly me to the moon "
|
|
|
|
<strong>Output:</strong> 4
|
|
|
|
<strong>Explanation:</strong> The last word is "moon" with length 4.
|
|
|
|
</pre>
|
|
|
|
|
2023-12-09 18:42:21 +08:00
|
|
|
<p><strong class="example">Example 3:</strong></p>
|
2022-03-27 18:35:17 +08:00
|
|
|
|
|
|
|
<pre>
|
|
|
|
<strong>Input:</strong> s = "luffy is still joyboy"
|
|
|
|
<strong>Output:</strong> 6
|
|
|
|
<strong>Explanation:</strong> The last word is "joyboy" with length 6.
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
<p> </p>
|
|
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
|
|
|
|
<ul>
|
|
|
|
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
|
|
|
|
<li><code>s</code> consists of only English letters and spaces <code>' '</code>.</li>
|
|
|
|
<li>There will be at least one word in <code>s</code>.</li>
|
|
|
|
</ul>
|