Skip to content

Commit 371713d

Browse files
committed
[Easy] Title: 58. Length of Last Word - LeetCode
1 parent 6bfe8c4 commit 371713d

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public int lengthOfLastWord(String s) {
3+
s = s.trim();
4+
int result = s.lastIndexOf(' ');
5+
6+
if(result == -1) {
7+
return s.length();
8+
}
9+
10+
return s.length() - 1 - result;
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# [Easy] 58. Length of Last Word
2+
3+
[문제 링크](https://leetcode.com/problems/length-of-last-word/)
4+
5+
### 문제 설명
6+
7+
Given a string s consisting of words and spaces, return the length of the last word in the string.
8+
9+
A word is a maximal substring consisting of non-space characters only.
10+
11+
12+
13+
Example 1:
14+
15+
```
16+
Input: s = "Hello World"
17+
Output: 5
18+
```
19+
Explanation: The last word is "World" with length 5.
20+
Example 2:
21+
22+
```
23+
Input: s = " fly me to the moon "
24+
Output: 4
25+
```
26+
Explanation: The last word is "moon" with length 4.
27+
Example 3:
28+
```
29+
Input: s = "luffy is still joyboy"
30+
Output: 6
31+
```
32+
Explanation: The last word is "joyboy" with length 6.
33+
34+
35+
Constraints:
36+
- 1 <= `s.length` <= 104
37+
- s consists of only English letters and spaces `' '`.
38+
- There will be at least one word in `s`.

0 commit comments

Comments
 (0)