summaryrefslogtreecommitdiff
path: root/src/bin/p0014.rs
diff options
context:
space:
mode:
authorMistivia <i@mistivia.com>2025-08-21 15:50:01 +0800
committerMistivia <i@mistivia.com>2025-08-21 15:50:01 +0800
commite575d56b149092c6a742aa3f90fe98b52e022f3e (patch)
tree9f030088e823b102459b50c8feb4b31c38abbf64 /src/bin/p0014.rs
parentba4b0d7e1448c575f78fdc17fb150cdaf282a093 (diff)
solve 14
Diffstat (limited to 'src/bin/p0014.rs')
-rw-r--r--src/bin/p0014.rs30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/bin/p0014.rs b/src/bin/p0014.rs
new file mode 100644
index 0000000..e85ef8f
--- /dev/null
+++ b/src/bin/p0014.rs
@@ -0,0 +1,30 @@
+impl Solution {
+ pub fn longest_common_prefix(strs: Vec<String>) -> String {
+ if strs.is_empty() { return "".to_string(); }
+ let mut l: i32 = -1;
+ let mut brk = false;
+ for i in 0..strs[0].len() {
+ let c = strs[0].as_bytes()[i];
+ for j in 1..strs.len() {
+ if strs[j].len() <= i || strs[j].as_bytes()[i] != c {
+ brk = true;
+ break;
+ }
+ }
+ if brk { break; }
+ l = l + 1;
+ }
+ if l < 0 {
+ return "".to_string();
+ }
+ strs[0][0..(l+1) as usize].to_string()
+ }
+}
+
+struct Solution {}
+fn main() {
+ println!("{}", Solution::longest_common_prefix(vec![
+ "fliower".to_string(),
+ "fliow".to_string(),
+ "fliight".to_string()]));
+} \ No newline at end of file