Longest Common Prefix in Rust

impl Solution {
    fn longest_common_prefix(strs: Vec<String>) -> String {
        // Handling the case of an empty vector
        if strs.is_empty() {
            return "".to_string();
        }

        // Handling the case of a vector with one string
        if strs.len() == 1 {
            return strs[0].clone();
        }

        let mut prefix = String::new();

        // Using the first string as a reference to find the common prefix
        let first_str = &strs[0];
        for (i, char) in first_str.chars().enumerate() {
            // Checking if the character is present at the same position in all strings
            for other_str in strs.iter().skip(1) {
                if i >= other_str.len() || other_str.chars().nth(i) != Some(char) {
                    // Return the prefix found so far if a mismatch occurs
                    return prefix;
                }
            }
            // Append the common character to the prefix
            prefix.push(char);
        }

        prefix
    }
}

Next