blob: 9e573c1c7d96610764498639ce1222b442f9f87a (
plain)
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
|
fn main() {
println!("Hello, world!");
let tests = [
(vec!["flower","flow","flight"], "fl"),
(vec!["dog","racecar","car"], "")
];
for test in tests {
println!("{:?} is {} should be {}", test.0,
Solution::longest_common_prefix(test.0.iter().map(|s| s.to_string()).collect()),
test.1);
}
}
struct Solution {}
impl Solution {
pub fn longest_common_prefix(strs: Vec<String>) -> String {
let mut index = 0;
let mut c = String::new();
loop {
for s in strs {
let prefix = s.chars().nth(index);
if prefix.is_none() {return c;}
}
break;
}
"".to_string()
}
}
|