Created
October 21, 2019 20:42
-
-
Save bDrwx/f2165d5b0d20e412d1e42600b8c8c3a2 to your computer and use it in GitHub Desktop.
14. Longest Common Prefix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
impl Solution { | |
pub fn longest_common_prefix(strs: Vec<String>) -> String { | |
if strs.len() == 0 { | |
return "".to_string(); | |
} | |
let mut prefix = strs[0].clone(); | |
for n in 1..strs.len() { | |
while(!strs[n].contains(&prefix)){ | |
prefix = prefix[..prefix.len()-1].to_string(); | |
} | |
} | |
return prefix | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment