-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# 0058. [Length of Last Word](https://leetcode.com/problems/length-of-last-word/description/?envType=study-plan-v2&envId=top-interview-150) | ||
|
||
Given a string s consisting of words and spaces, return _the length of the **last** word in the string._ | ||
|
||
A **word** is a maximal substring consisting of non-space characters only. | ||
|
||
### **Example 1:** | ||
|
||
<pre><code> | ||
<strong>Input:</strong> s = "Hello World" | ||
<strong>Output:</strong> 5 | ||
<strong>Explanation:</strong> The last word is "World" with length 5. | ||
</code></pre> | ||
|
||
### **Example 2:** | ||
|
||
<pre><code> | ||
<strong>Input:</strong> s = " fly me to the moon " | ||
<strong>Output:</strong> 4 | ||
<strong>Explanation:</strong> The last word is "moon" with length 4. | ||
</code></pre> | ||
|
||
### **Example 3:** | ||
|
||
<pre><code> | ||
<strong>Input:</strong> s = "luffy is still joyboy" | ||
<strong>Output:</strong> 6 | ||
<strong>Explanation:</strong> The last word is "joyboy" with length 6. | ||
</code></pre> | ||
|
||
### **Constraints:** | ||
|
||
- <code>1 <= s.length <= 10<sup>4</sup></code> | ||
- `s` consists of only English letters and spaces `' '`. | ||
- There will be at least one word in `s`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { lengthOfLastWord } from '.'; | ||
|
||
describe('0058. Length of Last Word', () => { | ||
interface TestCase { | ||
input: { | ||
s: string; | ||
}; | ||
expected: number; | ||
} | ||
|
||
const testCases: TestCase[] = [ | ||
{ | ||
input: { | ||
s: 'Hello World', | ||
}, | ||
expected: 5, | ||
}, | ||
{ | ||
input: { | ||
s: ' fly me to the moon ', | ||
}, | ||
expected: 4, | ||
}, | ||
{ | ||
input: { | ||
s: 'luffy is still joyboy', | ||
}, | ||
expected: 6, | ||
}, | ||
]; | ||
|
||
test.each(testCases)('Case %#', ({ input: { s }, expected }) => { | ||
const result = lengthOfLastWord(s); | ||
expect(result).toBe(expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
function lengthOfLastWord(s: string): number {} | ||
|
||
export { lengthOfLastWord }; |