leetcode

Log | Files | Refs | README

commit 5ff1026e41efe5bdb7b01481f90c440df5d0add6
parent 568e811275f446c89491c86fdccf363fbeebdb8d
Author: ling0x <ling0x@users.noreply.github.com>
Date:   Tue, 21 Apr 2026 20:47:13 +0100

exercise in logic

Diffstat:
Asrc/array/longest_common_prefix.rs | 72++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/array/mod.rs | 1+
Msrc/lib.rs | 2++
Msrc/main.rs | 16++++++++--------
4 files changed, 83 insertions(+), 8 deletions(-)

diff --git a/src/array/longest_common_prefix.rs b/src/array/longest_common_prefix.rs @@ -0,0 +1,72 @@ +use std::collections::HashSet; + +use tracing::info; + +pub fn longest_common_prefix(strs: Vec<String>) -> String { + let mut seen = HashSet::<char>::new(); + 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); + // } + } + } + + for cc in &common { + let result = seen_seen.insert(cc.to_owned()); + if !result { + common_common.push(cc.to_owned()); + } + } + + info!("Seen: {:?}", seen); + 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() +} + +#[cfg(test)] +mod tests { + + #[test] + fn longest_common_prefix_1_cases() { + assert!(true) + } +} diff --git a/src/array/mod.rs b/src/array/mod.rs @@ -0,0 +1 @@ +pub mod longest_common_prefix; diff --git a/src/lib.rs b/src/lib.rs @@ -1,3 +1,5 @@ +pub mod array; pub mod hash_table; + #[cfg(test)] pub mod test_utils; diff --git a/src/main.rs b/src/main.rs @@ -1,15 +1,15 @@ -use leetcode::hash_table::two_sum::two_sum_9; +use leetcode::{ + array::longest_common_prefix::longest_common_prefix, hash_table::two_sum::two_sum_9, +}; use tracing::info; use tracing_subscriber::fmt; fn main() { fmt().with_target(false).compact().init(); info!("-----------------------"); - two_sum_9(vec![2, 7, 11, 15], 9); - info!("-----------------------"); - two_sum_9(vec![3, 2, 3], 6); - info!("-----------------------"); - two_sum_9(vec![3, 2, 4], 6); - info!("-----------------------"); - two_sum_9(vec![-3, 4, 3, 90], 0); + longest_common_prefix(vec![ + "flower".to_string(), + "flow".to_string(), + "flight".to_string(), + ]); }