commit f240500d976c7dd3c34bb77c458c4203cc22d532
parent 5ff1026e41efe5bdb7b01481f90c440df5d0add6
Author: ling0x <ling0x@users.noreply.github.com>
Date: Tue, 21 Apr 2026 23:42:38 +0100
exercise in logic
Diffstat:
1 file changed, 2 insertions(+), 30 deletions(-)
diff --git a/src/array/longest_common_prefix.rs b/src/array/longest_common_prefix.rs
@@ -7,39 +7,14 @@ pub fn longest_common_prefix(strs: Vec<String>) -> String {
let mut seen_seen = HashSet::<char>::new();
let mut common = Vec::<char>::new();
let mut common_common = Vec::<char>::new();
- let mut unique = Vec::<char>::new();
for word in strs {
- info!("{word}");
let chars = word.chars().collect::<Vec<char>>();
for c in chars.iter() {
let result = seen.insert(c.to_owned());
if !result {
common.push(c.to_owned());
- } else {
- unique.push(c.to_owned());
}
- // info!("{c}");
- // // f l o w e r
- // // f l o w
- // // f l i g h t
- // let already_seen = seen.iter().find(|x| x == &c);
- // if let Some(v) = already_seen {
- // info!("Already seen, adding to array: {}", v);
- // common.push(c.to_owned());
- // } else {
- // info!("Not seen: {}", c);
- // }
- // seen.push(c.to_owned());
- // info!("Seen: {:?}", seen);
- // info!("Common: {:?}", common);
-
- // let is_in_common = common.iter().find(|x| x == &c);
- // if let Some(v) = is_in_common {
- // info!("Already in common: {}", v);
- // } else {
- // info!("Not in common: {}", c);
- // }
}
}
@@ -54,12 +29,9 @@ pub fn longest_common_prefix(strs: Vec<String>) -> String {
info!("Common: {:?}", common);
info!("Seen seen: {:?}", seen_seen);
info!("Common common: {:?}", common_common);
- // info!("Unique: {:?}", unique);
- // info!("All characters: {:?}", all_characters);
-
- // let chars: Vec<Vec<_>> = strs.into_iter().map(|x| x.chars()).collect();
- "prefix".to_string()
+ let result = String::from_iter(common_common);
+ result
}
#[cfg(test)]