mirror of
https://github.com/devenperez/leetcode.git
synced 2026-06-13 14:57:08 +00:00
35 lines
1.2 KiB
Markdown
35 lines
1.2 KiB
Markdown
<h2><a href="https://leetcode.com/problems/length-of-last-word/">58. Length of Last Word</a></h2><h3>Easy</h3><hr><div><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>
|
|
|
|
<p>A <strong>word</strong> is a maximal substring consisting of non-space characters only.</p>
|
|
|
|
<p> </p>
|
|
<p><strong>Example 1:</strong></p>
|
|
|
|
<pre><strong>Input:</strong> s = "Hello World"
|
|
<strong>Output:</strong> 5
|
|
<strong>Explanation:</strong> The last word is "World" with length 5.
|
|
</pre>
|
|
|
|
<p><strong>Example 2:</strong></p>
|
|
|
|
<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>
|
|
|
|
<p><strong>Example 3:</strong></p>
|
|
|
|
<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>
|
|
</div> |