#953
Easy Algorithms Verifying an alien dictionary
Array Hash Table String
55.9% acceptance
Feb 25, 2026
5070
1681
In an alien language, surprisingly, they also use English lowercase letters, but possibly in a different order. The order of the alphabet is some permutation of lowercase letters.
Given a sequence of words written in the alien language, and the order of the alphabet, return true if and only if the given words are sorted lexicographically in this alien language.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn is_alien_sorted(words: Vec<String>, order: String) -> bool {
let mut rank = [0u8; 26];
for (i, c) in order.bytes().enumerate() { rank[(c - b'a') as usize] = i as u8; }
for w in words.windows(2) {
let a: Vec<u8> = w[0].bytes().map(|c| rank[(c - b'a') as usize]).collect();
let b: Vec<u8> = w[1].bytes().map(|c| rank[(c - b'a') as usize]).collect();
if a > b { return false; }
}
true
}
}