-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.rs
56 lines (52 loc) · 1.52 KB
/
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
fn main() {
println!("Hello, world!");
Solution::longest_common_prefix(vec![String::from("s: &str")]);
}
struct Solution {}
impl Solution {
pub fn longest_common_prefix(strs: Vec<String>) -> String {
let mut ret = "".to_string();
let mut cnt = 0;
let mut found = false;
if strs.len() == 0 || strs[0].len() == 0 {
return ret
}
loop {
ret.push_str(&strs[0][cnt..cnt + 1]);
for i in 0..strs.len() {
if strs[i].len() < cnt + 1 || strs[i][0..cnt + 1] != ret {
found = true;
break;
}
}
match found {
true => break ret[0..cnt].to_string(),
false => if cnt + 1 == strs[0].len() {
break ret
}
}
cnt += 1;
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn basic() {
assert_eq!(Solution::longest_common_prefix(
vec![String::from("s: &str"),
String::from("s: &str")]),
String::from("s: &str"));
assert_eq!(Solution::longest_common_prefix(
vec![String::from("flower"),
String::from("flow"),
String::from("flight")]),
String::from("fl"));
assert_eq!(Solution::longest_common_prefix(
vec![String::from("dog"),
String::from("racecar"),
String::from("flight")]),
String::from(""));
}
}