Skip to content

Commit 834bd0c

Browse files
committed
LeetCode length-of-last-word
1 parent e675b10 commit 834bd0c

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

LeetCode/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# LeetCode
2+
3+
```
4+
leetup pick X -l rust
5+
cargo +nightly -Zscript best-time-to-buy-and-sell-stock.rs
6+
```

LeetCode/length-of-last-word.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// @leetup=custom
2+
// @leetup=info id=58 lang=rust slug=length-of-last-word
3+
4+
/*
5+
* Given a string `s` consisting of words and spaces, return *the length of the
6+
* last word in the string.*
7+
*
8+
* A word is a maximal substring consisting of non-space characters only.
9+
*
10+
*
11+
* Example 1:
12+
*
13+
* Input: s = "Hello World"
14+
* Output: 5
15+
* Explanation: The last word is "World" with length 5.
16+
*
17+
* Example 2:
18+
*
19+
* Input: s = " fly me to the moon "
20+
* Output: 4
21+
* Explanation: The last word is "moon" with length 4.
22+
*
23+
* Example 3:
24+
*
25+
* Input: s = "luffy is still joyboy"
26+
* Output: 6
27+
* Explanation: The last word is "joyboy" with length 6.
28+
*
29+
*
30+
* Constraints:
31+
*
32+
* * `1 <= s.length <= 104`
33+
* * `s` consists of only English letters and spaces `' '`.
34+
* * There will be at least one word in `s`.
35+
*
36+
*/
37+
// @leetup=custom
38+
// @leetup=inject:before_code_ex
39+
// Test comment
40+
// Test code
41+
// @leetup=inject:before_code_ex
42+
43+
// @leetup=code
44+
45+
// @leetup=inject:before_code
46+
// @leetup=inject:before_code
47+
48+
impl Solution {
49+
pub fn length_of_last_word(s: String) -> i32 {
50+
s.split_whitespace().last().unwrap().len() as i32
51+
}
52+
}
53+
// @leetup=code
54+
55+
// @leetup=inject:after_code
56+
57+
struct Solution;
58+
59+
fn main() {
60+
let input = "Hello World".to_string();
61+
let solution = Solution::length_of_last_word(input);
62+
println!("The length of the last word is: {}", solution);
63+
}
64+
65+
// @leetup=inject:after_code

0 commit comments

Comments
 (0)