use std::cmp;
pub fn longest_common_prefix(strings: Vec<&str>) -> &str {
if strings.is_empty() {
return "";
}
let str0 = strings[0];
let str0bytes = str0.as_bytes();
let mut len = str0.len();
for str in &strings[1..] {
len = cmp::min(len,
str.as_bytes()
.iter()
.zip(str0bytes)
.take_while(|&(a, b)| a == b)
.count());
}
&strings[0][..len]
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn empty_lcp() {
assert_eq!(longest_common_prefix(vec![]), "");
}
#[test]
fn single_lcp() {
assert_eq!(longest_common_prefix(vec!["ab"]), "ab");
}
#[test]
fn no_lcp() {
assert_eq!(longest_common_prefix(vec!["a", "b", "c"]), "");
}
#[test]
fn valid_lcp() {
assert_eq!(longest_common_prefix(vec!["aba", "abb", "abc"]), "ab");
}
#[test]
fn valid_is_shortest_lcp() {
assert_eq!(longest_common_prefix(vec!["aba", "ab", "abc"]), "ab");
}
}