LeetCode 953. Verifying an Alien Dictionary

Problem Statement


use std::collections::HashMap;

impl Solution {
fn compare(w1: &String, w2: &String, map: &HashMap<char, usize>) -> bool {
let size = std::cmp::min(w1.len(), w2.len());
for i in 0..size {
let (i1, i2) = (
*map.get(&w1.chars().nth(i).unwrap()).unwrap(),
*map.get(&w2.chars().nth(i).unwrap()).unwrap());
if i1 > i2 {
return false;
} else if i1 == i2 {
continue;
} else {
return true;
}
}

if w1.len() > w2.len() {
return false;
}

return true;
}
pub fn is_alien_sorted(words: Vec<String>, order: String) -> bool {
let mut map = HashMap::new();
for i in 0..order.len() {
map.insert(order.chars().nth(i).unwrap(), i);
}
for i in 0..words.len() - 1 {
if Self::compare(&words[i], &words[i + 1], &map) {
continue;
}
return false;
}
return true;
}
}