#953 Verifying an Alien Dictionary
the problem
In plain words
An alien language uses the 26 lowercase letters in a different order, given as order. Return true if words are sorted lexicographically in that alien order.
in words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
out true
'h' comes before 'l' in this language.
in words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
out false
'd' comes after 'l', so "word" > "world".
in words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz"
out false
A word must come after its own prefix.
- 1 ≤ words.length ≤ 100
- 1 ≤ words[i].length ≤ 20
- order.length == 26
your turn
Try it before you peek
Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.
Solve it in the browser
LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.
Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.
Marking the problem solved, or opening every hint, also shows it.
the aha
Key insight
patternCompare at the first difference
Two words are ordered by their first differing letter; if there is none, the shorter one must come first. Checking adjacent pairs is enough because sorted order is transitive.
solutions
From brute force to optimal
Map each alien letter to its rank as 'a'..'z', rewrite every word, and check the rewritten list is already sorted.
- Build letter → normal letter map.
- Rewrite all words.
- Sort a copy and compare with the original.
S = total characters to rewrite; sorting N words does N log N comparisons of up to L characters.
Rewritten copies of every word.
class Solution { /** Rewrite every word into normal letters ('a' = alien first letter), then check sortedness. */ public boolean isAlienSorted(String[] words, String order) { char[] map = new char[26]; for (int i = 0; i < 26; i++) map[order.charAt(i) - 'a'] = (char) ('a' + i); String[] translated = new String[words.length]; for (int w = 0; w < words.length; w++) { StringBuilder sb = new StringBuilder(); for (char ch : words[w].toCharArray()) sb.append(map[ch - 'a']); translated[w] = sb.toString(); } String[] sorted = translated.clone(); Arrays.sort(sorted); return Arrays.equals(sorted, translated); }}rank[c] = position of c in order. Compare each word only with the next one.
- Fill rank[26].
- For each adjacent pair find the first differing letter and compare ranks.
- No difference → the first word must not be longer.
Each character is compared at most twice (with its left and right neighbour word).
A fixed 26-entry array.
class Solution { public boolean isAlienSorted(String[] words, String order) { int[] rank = new int[26]; for (int i = 0; i < 26; i++) rank[order.charAt(i) - 'a'] = i; for (int w = 0; w + 1 < words.length; w++) { if (!inOrder(words[w], words[w + 1], rank)) return false; } return true; // sortedness only needs ADJACENT pairs (it is transitive) } private boolean inOrder(String a, String b, int[] rank) { int len = Math.min(a.length(), b.length()); for (int i = 0; i < len; i++) { char x = a.charAt(i), y = b.charAt(i); if (x != y) return rank[x - 'a'] < rank[y - 'a']; // first difference decides } return a.length() <= b.length(); // "apple" before "app" is wrong }}in the room
How to say it in the interview
talk track
- State the two rules: first difference decides; otherwise prefix comes first.
- Explain why adjacent pairs suffice (transitivity), then code the rank array.
traps
- Forgetting the prefix case ["apple","app"].
- Comparing every pair (O(N²·L)) when adjacent pairs suffice.
what they ask next
Follow-ups
Now the order is unknown — recover it from the sorted words.
That's LC 269 Alien Dictionary: each first difference becomes an edge, then topological sort.
before you submit
Edge cases to test
- Prefix: ["apple", "app"] is not sorted.
- Two equal adjacent words are fine.
- A single word is always sorted.
practise next
Similar problems
- #269Alien DictionaryHardin Topo SortLeetCode Premium
- #14Longest Common PrefixEasyin Arrays & Strings