-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.rs
29 lines (24 loc) · 830 Bytes
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
fn main() {
assert_eq!(Solution::length_of_last_word(String::from("Hello World")), 5);
}
struct Solution {}
impl Solution {
pub fn length_of_last_word(s: String) -> i32 {
let s = s.trim();
if let Some(idx) = s.rfind(' ') {
(s.len() - 1 - idx) as i32
} else { if s.len() > 0 { s.len() as i32 } else { 0 } }
}
}
#[cfg(test)]
mod test {
use crate::*;
#[test]
fn basic() {
assert_eq!(Solution::length_of_last_word(String::from("Hello World")), 5);
assert_eq!(Solution::length_of_last_word(String::from("")), 0);
assert_eq!(Solution::length_of_last_word(String::from("Hello")), 5);
assert_eq!(Solution::length_of_last_word(String::from("H ")), 1);
assert_eq!(Solution::length_of_last_word(String::from(" ")), 0);
}
}