Skip to content

Commit

Permalink
problem: 0058. Length of Last Word
Browse files Browse the repository at this point in the history
  • Loading branch information
flynnpark committed Jan 29, 2024
1 parent c7a254e commit cf01b01
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/0058/README.md
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`.
36 changes: 36 additions & 0 deletions src/0058/index.test.ts
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);
});
});
3 changes: 3 additions & 0 deletions src/0058/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function lengthOfLastWord(s: string): number {}

export { lengthOfLastWord };

0 comments on commit cf01b01

Please sign in to comment.