Skip to content

Commit

Permalink
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 2 MB (88.…
Browse files Browse the repository at this point in the history
…07%)
  • Loading branch information
hucancode committed Apr 1, 2024
1 parent 0f30df5 commit b5137f2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
37 changes: 37 additions & 0 deletions leetcode/0058-length-of-last-word/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<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 <span data-keyword="substring-nonempty">substring</span> consisting of non-space characters only.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;Hello World&quot;
<strong>Output:</strong> 5
<strong>Explanation:</strong> The last word is &quot;World&quot; with length 5.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot; fly me to the moon &quot;
<strong>Output:</strong> 4
<strong>Explanation:</strong> The last word is &quot;moon&quot; with length 4.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;luffy is still joyboy&quot;
<strong>Output:</strong> 6
<strong>Explanation:</strong> The last word is &quot;joyboy&quot; with length 6.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li>
<li><code>s</code> consists of only English letters and spaces <code>&#39; &#39;</code>.</li>
<li>There will be at least one word in <code>s</code>.</li>
</ul>
5 changes: 5 additions & 0 deletions leetcode/0058-length-of-last-word/solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
impl Solution {
pub fn length_of_last_word(s: String) -> i32 {
s.split_whitespace().last().map_or(0, str::len) as i32
}
}

0 comments on commit b5137f2

Please sign in to comment.