Verifying an Alien Dictionary - Problem

Imagine you're working as a linguist studying an alien civilization that has just made contact with Earth. Surprisingly, these aliens use the same English lowercase letters as we do, but they've arranged their alphabet in a completely different order!

You've been given two crucial pieces of information:

  • A sequence of words written in this alien language
  • The order of their alphabet (a permutation of our 26 lowercase letters)

Your mission is to determine whether the given words are sorted lexicographically according to the alien alphabet order. In other words, would these words appear in the correct order in an alien dictionary?

For example: If the alien alphabet order is "hlabcdefgijkmnopqrstuvwxyz" (where 'h' comes before 'a'), then the word "hello" would come before "apple" in their dictionary, which is opposite to our English ordering!

Return true if the words are properly sorted according to the alien alphabet, false otherwise.

Input & Output

example_1.py โ€” Basic Valid Case
$ Input: words = ["hello", "leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
โ€บ Output: true
๐Ÿ’ก Note: In the alien alphabet, 'h' comes before 'l', so 'hello' should come before 'leetcode'. The first differing characters are 'h' vs 'l', and since h is at position 0 and l is at position 1 in the alien order, this is correct.
example_2.py โ€” Invalid Order Case
$ Input: words = ["word", "world", "row"], order = "worldabcefghijkmnpqstuvxyz"
โ€บ Output: false
๐Ÿ’ก Note: The words are not sorted because 'world' should not come after 'word'. When comparing 'word' and 'world', all characters match until we reach the end of 'word'. Since 'word' is shorter, it should come first, but 'world' comes after it in the input, making this correct so far. However, 'world' vs 'row': 'w' comes after 'r' in the alien order, so 'row' should come before 'world'.
example_3.py โ€” Single Character Difference
$ Input: words = ["apple", "app"], order = "abcdefghijklmnopqrstuvwxyz"
โ€บ Output: false
๐Ÿ’ก Note: This is invalid because 'app' should come before 'apple' (shorter word comes first when one is a prefix of another), but in the given order 'apple' comes first.

Visualization

Tap to expand
Alien Dictionary VerificationAlien Alphabet Orderh l a b c d e f g i j k m n o p q r s t u v w x y zWords to Verify: ["hello", "leetcode"]helloleetcodecompareCharacter ComparisonPosition of 'h' in alien order: 0Position of 'l' in alien order: 1Since 0 < 1, "hello" comes before "leetcode" โœ“Resultโœ“Words are sorted correctly!Time Complexity: O(N ร— M)N = number of words, M = average word lengthSpace Complexity: O(1) - hash map stores 26 characters
Understanding the Visualization
1
Build Reference Guide
Create a position map for the alien alphabet: hโ†’0, lโ†’1, aโ†’2, bโ†’3, etc.
2
Compare Adjacent Books
Check each pair of adjacent words to ensure they follow the alien ordering
3
Find First Difference
For each word pair, find the first position where characters differ
4
Verify Order
Use the position map to check if the first word's character comes before the second word's character
Key Takeaway
๐ŸŽฏ Key Insight: We only need to compare adjacent word pairs, and for each pair, we find the first differing character position to determine the correct lexicographical order using the alien alphabet mapping.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(N * M)

N is number of words, M is average word length. Hash map lookups are O(1)

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Hash map stores at most 26 character positions (constant space)

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค words.length โ‰ค 100
  • 1 โ‰ค words[i].length โ‰ค 20
  • order.length == 26
  • All characters in words[i] and order are English lowercase letters
  • order is a permutation of the English lowercase letters
Asked in
Google 45 Facebook 32 Amazon 28 Microsoft 18
125.0K Views
High Frequency
~15 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen