diff --git a/src/0058/README.md b/src/0058/README.md new file mode 100644 index 0000000..707a761 --- /dev/null +++ b/src/0058/README.md @@ -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:** + +

+Input: s = "Hello World"
+Output: 5
+Explanation: The last word is "World" with length 5.
+
+ +### **Example 2:** + +

+Input: s = "   fly me   to   the moon  "
+Output: 4
+Explanation: The last word is "moon" with length 4.
+
+ +### **Example 3:** + +

+Input: s = "luffy is still joyboy"
+Output: 6
+Explanation: The last word is "joyboy" with length 6.
+
+ +### **Constraints:** + +- 1 <= s.length <= 104 +- `s` consists of only English letters and spaces `' '`. +- There will be at least one word in `s`. diff --git a/src/0058/index.test.ts b/src/0058/index.test.ts new file mode 100644 index 0000000..a9d0eba --- /dev/null +++ b/src/0058/index.test.ts @@ -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); + }); +}); diff --git a/src/0058/index.ts b/src/0058/index.ts new file mode 100644 index 0000000..b2b5063 --- /dev/null +++ b/src/0058/index.ts @@ -0,0 +1,3 @@ +function lengthOfLastWord(s: string): number {} + +export { lengthOfLastWord };