commit 9224a655cc914f60cc715389f4159d0915b510a3
parent 73874e5eb74637d49baec2b0402acc0706bfa5ec
Author: ling0x <ling0x@users.noreply.github.com>
Date: Sun, 26 Jul 2026 17:11:13 +0100
update
Diffstat:
1 file changed, 4 insertions(+), 6 deletions(-)
diff --git a/src/array/longest_common_prefix.rs b/src/array/longest_common_prefix.rs
@@ -115,7 +115,7 @@ fn solution_2(strs: Vec<String>) -> String {
// Third try: BTreeMap is automatically sorted
// 0ms, 2.33mb
fn solution_3(strs: Vec<String>) -> String {
- let mut common = Vec::<(usize, char)>::new();
+ let mut common = Vec::<char>::new();
for (index, character) in strs
.iter()
@@ -123,16 +123,14 @@ fn solution_3(strs: Vec<String>) -> String {
.collect::<BTreeMap<usize, char>>()
{
info!("{index}: {character}");
- let existing_prefix = String::from_iter(common.iter().map(|(_, char)| char));
+ let existing_prefix = String::from_iter(&common);
let prefix = format!("{existing_prefix}{character}");
if strs.iter().all(|x| x.starts_with(&prefix)) {
- common.push((index, character));
+ common.push(character);
}
}
- info!("Common: {common:?}");
-
- String::from_iter(common.iter().map(|(_, char)| char))
+ String::from_iter(common)
}
#[cfg(test)]