diff options
author | Orangerot <purple@orangerot.dev> | 2024-06-27 11:30:16 +0200 |
---|---|---|
committer | Orangerot <purple@orangerot.dev> | 2024-06-27 11:30:16 +0200 |
commit | 4b0a6a01b051a4ebfbc17661d14cb23fe4f275fb (patch) | |
tree | 0072cca328fe5adb2ed61004010228ff85e2164d /longest-common-prefix |
Diffstat (limited to 'longest-common-prefix')
-rw-r--r-- | longest-common-prefix/Cargo.toml | 8 | ||||
-rw-r--r-- | longest-common-prefix/src/main.rs | 30 |
2 files changed, 38 insertions, 0 deletions
diff --git a/longest-common-prefix/Cargo.toml b/longest-common-prefix/Cargo.toml new file mode 100644 index 0000000..91d0dc9 --- /dev/null +++ b/longest-common-prefix/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "longest-common-prefix" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/longest-common-prefix/src/main.rs b/longest-common-prefix/src/main.rs new file mode 100644 index 0000000..9e573c1 --- /dev/null +++ b/longest-common-prefix/src/main.rs @@ -0,0 +1,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() + } +} + |