Skip to content

Commit b7de3a2

Browse files
feat: add problem 58 length of last word
1 parent 23a61e2 commit b7de3a2

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed

explanations/58/en.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# 58. Length of Last Word
2+
3+
**Difficulty:** Easy
4+
**Link:** https://leetcode.com/problems/length-of-last-word/
5+
6+
## Problem Description
7+
8+
Given a string `s` consisting of words and spaces, return *the length of the **last** word in the string.*
9+
10+
A **word** is a maximal substring consisting of non-space characters only.
11+
12+
**Example 1:**
13+
```
14+
Input: s = "Hello World"
15+
Output: 5
16+
Explanation: The last word is "World" with length 5.
17+
```
18+
19+
**Example 2:**
20+
```
21+
Input: s = " fly me to the moon "
22+
Output: 4
23+
Explanation: The last word is "moon" with length 4.
24+
```
25+
26+
**Example 3:**
27+
```
28+
Input: s = "luffy is still joyboy"
29+
Output: 6
30+
Explanation: The last word is "joyboy" with length 6.
31+
```
32+
33+
**Constraints:**
34+
- `1 <= s.length <= 10^4`
35+
- `s` consists of only English letters and spaces `' '`.
36+
- There will be at least one word in `s`.
37+
38+
## Explanation
39+
40+
### Strategy
41+
42+
This is a **string manipulation problem** that requires finding the length of the last word in a string. The key insight is to iterate from the end of the string and count characters until we encounter a space.
43+
44+
**Key observations:**
45+
- We need to handle trailing spaces
46+
- We need to find the last word by starting from the end
47+
- We can skip trailing spaces and count characters until we hit a space
48+
- We need to handle the case where the last word might be at the beginning
49+
50+
**High-level approach:**
51+
1. **Start from the end**: Iterate from the last character
52+
2. **Skip trailing spaces**: Move backwards until we find a non-space character
53+
3. **Count characters**: Count characters until we hit a space or reach the beginning
54+
4. **Return count**: Return the length of the last word
55+
56+
### Steps
57+
58+
Let's break down the solution step by step:
59+
60+
**Step 1: Initialize variables**
61+
- `length = 0`: Track the length of the last word
62+
- `i = len(s) - 1`: Start from the last character
63+
64+
**Step 2: Skip trailing spaces**
65+
- Move backwards until we find a non-space character
66+
67+
**Step 3: Count characters**
68+
- Count characters until we hit a space or reach the beginning
69+
70+
**Step 4: Return result**
71+
- Return the count
72+
73+
**Example walkthrough:**
74+
Let's trace through the second example:
75+
76+
```
77+
s = " fly me to the moon "
78+
79+
Step 1: Start from end
80+
i = 23 (last character is space)
81+
82+
Step 2: Skip trailing spaces
83+
i = 22 (space)
84+
i = 21 (space)
85+
i = 20 (space)
86+
i = 19 ('n' - found non-space character)
87+
88+
Step 3: Count characters
89+
length = 1 ('n')
90+
i = 18, length = 2 ('o')
91+
i = 17, length = 3 ('o')
92+
i = 16, length = 4 ('m')
93+
i = 15 (space - stop counting)
94+
95+
Result: Return 4
96+
```
97+
98+
> **Note:** The key insight is to work backwards from the end of the string. This approach is efficient because we only need to traverse the string once from the end, and we can handle trailing spaces naturally.
99+
100+
### Solution
101+
102+
```python
103+
class Solution:
104+
def lengthOfLastWord(self, s: str) -> int:
105+
# Start from the end of the string
106+
i = len(s) - 1
107+
length = 0
108+
109+
# Skip trailing spaces
110+
while i >= 0 and s[i] == ' ':
111+
i -= 1
112+
113+
# Count characters of the last word
114+
while i >= 0 and s[i] != ' ':
115+
length += 1
116+
i -= 1
117+
118+
# Return the length of the last word
119+
return length
120+
```
121+
122+
**Time Complexity:** O(n) - we visit each character at most once
123+
**Space Complexity:** O(1) - we only use a constant amount of extra space

solutions/58/01.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def lengthOfLastWord(self, s: str) -> int:
3+
# Start from the end of the string
4+
i = len(s) - 1
5+
length = 0
6+
7+
# Skip trailing spaces
8+
while i >= 0 and s[i] == " ":
9+
i -= 1
10+
11+
# Count characters of the last word
12+
while i >= 0 and s[i] != " ":
13+
length += 1
14+
i -= 1
15+
16+
# Return the length of the last word
17+
return length

0 commit comments

Comments
 (0)