Skip to content

Commit 1516240

Browse files
authored
Merge pull request #513 from thevijayshankersharma/0058-solution
Add a Solution for Length of Last Word (LeetCode Problem 58)
2 parents 91c9222 + 5dd976c commit 1516240

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
---
2+
id: length-of-last-word
3+
title: Length of Last Word (LeetCode)
4+
difficulty: Easy
5+
sidebar_label: 0058-LengthOfLastWord
6+
topics:
7+
- String
8+
---
9+
10+
## Problem Description
11+
12+
| Problem Statement | Solution Link | LeetCode Profile |
13+
| :---------------- | :------------ | :--------------- |
14+
| [Merge Two Sorted Lists](https://leetcode.com/problems/length-of-last-word/) | [Merge Two Sorted Lists Solution on LeetCode](https://leetcode.com/problems/length-of-last-word/solutions/) | [VijayShankerSharma](https://leetcode.com/u/darkknight648/) |
15+
16+
17+
## Problem Description
18+
19+
Given a string `s` consisting of words and spaces, return the length of the last word in the string.
20+
21+
### Examples
22+
23+
#### Example 1
24+
25+
- **Input:** s = "Hello World"
26+
- **Output:** 5
27+
- **Explanation:** The last word is "World" with length 5.
28+
29+
#### Example 2
30+
31+
- **Input:** s = " fly me to the moon "
32+
- **Output:** 4
33+
- **Explanation:** The last word is "moon" with length 4.
34+
35+
#### Example 3
36+
37+
- **Input:** s = "luffy is still joyboy"
38+
- **Output:** 6
39+
- **Explanation:** The last word is "joyboy" with length 6.
40+
41+
### Constraints
42+
43+
- `1 <= s.length <= 10^4`
44+
- `s` consists of only English letters and spaces ' '.
45+
- There will be at least one word in `s`.
46+
47+
### Approach
48+
49+
To find the length of the last word in the string `s`, we can follow these steps:
50+
51+
1. Trim any trailing spaces from the string.
52+
2. Initialize a variable `length` to store the length of the last word.
53+
3. Iterate over the string from right to left:
54+
- When encountering a non-space character, increment `length`.
55+
- Break the loop when encountering the first space character after a non-space character.
56+
4. Return `length`.
57+
58+
### Solution Code
59+
60+
#### Python
61+
62+
```
63+
class Solution(object):
64+
def lengthOfLastWord(self, s):
65+
return len(s.split()[-1])
66+
```
67+
68+
#### C++
69+
70+
```
71+
class Solution {
72+
public:
73+
int lengthOfLastWord(string s) {
74+
int length = 0;
75+
int n = s.length();
76+
while (n > 0 && s[n - 1] == ' ') {
77+
n--; // Trim trailing spaces
78+
}
79+
for (int i = n - 1; i >= 0; --i) {
80+
if (s[i] != ' ') {
81+
length++;
82+
} else if (length != 0) {
83+
break;
84+
}
85+
}
86+
return length;
87+
}
88+
};
89+
```
90+
91+
#### Java
92+
93+
```
94+
class Solution {
95+
public int lengthOfLastWord(String s) {
96+
int length = 0;
97+
s = s.trim(); // Trim trailing spaces
98+
for (int i = s.length() - 1; i >= 0; --i) {
99+
if (s.charAt(i) != ' ') {
100+
length++;
101+
} else if (length != 0) {
102+
break;
103+
}
104+
}
105+
return length;
106+
}
107+
}
108+
```
109+
110+
### Conclusion
111+
112+
The Length of Last Word problem can be efficiently solved by trimming any trailing spaces from the string and iterating over the string from right to left to find the length of the last word. The provided solution code implements this approach in Python, C++, and Java, providing an optimal solution to the problem.

0 commit comments

Comments
 (0)