↓Skip to main content
  1. LeetCode/

LeetCode 953: Verifying an Alien Dictionary

·2 mins· ·
LeetCode Easy Array Hash-Table String
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-09-26

  • Submissions: Wrong Answer → Runtime Error ×4 → Wrong Answer → Runtime Error ×3 → Accepted ×3

Approach
#

Build rank[c], each letter’s position in order (Hash Table). Lexicographic order only needs adjacent pairs: the first differing character decides by rank, and if one word is a prefix of the other, the shorter must come first. In Python, comparing lists of ranks does exactly this, since list comparison is lexicographic.

My Attempts
#

This took 12 submissions:

  • Attempt 1 compared column by column with zip(*words), requiring every column to be non-decreasing, although only the first differing character matters; zip also truncates, so prefixes went unchecked: ["ll", "l"] returned True.
  • Switching to adjacent-pair comparison, the outer loop first ran to the last word and words[i + 1] went out of range (IndexError).
  • One version used range(len(words[i]) - 1) and skipped the last character: ["r", "p"] compared nothing and returned True.
  • After that, a shorter next word still made words[i + 1][j] raise IndexError. Returning False once the next word runs out passed.

Solution
#

class Solution:
    def isAlienSorted(self, words: List[str], order: str) -> bool:
        rank = {c: i for i, c in enumerate(order)}
        keys = [[rank[c] for c in w] for w in words]
        return all(keys[i] <= keys[i + 1] for i in range(len(keys) - 1))

Complexity
#

Time O(total characters), space O(total characters).

Mistakes to Avoid
#

  • Only the first differing character matters.
  • The prefix case: ["ll", "l"] is not sorted.
  • Check the next word’s length before reading its characters.

Interview Explanation
#

I map letters to their alien positions, convert words into lists of positions, and check each adjacent pair is non-decreasing; list comparison is lexicographic and handles prefixes. Time is linear in the total length.

Related

LeetCode 929: Unique Email Addresses
·2 mins
LeetCode Easy Array Hash-Table String
LeetCode 929 note: split at @, normalize only the local part (drop everything after +, remove dots), rejoin with the domain and collect in a set
LeetCode 819: Most Common Word
·2 mins
LeetCode Easy Array Hash-Table String Counting
LeetCode 819 note: replace punctuation with spaces, lowercase, split, count, and pick the most frequent word not banned
LeetCode 748: Shortest Completing Word
·2 mins
LeetCode Easy Array Hash-Table String
LeetCode 748 note: count the plate’s letters in lowercase, then find the shortest word covering every count, taking the earliest on ties
LeetCode 599: Minimum Index Sum of Two Lists
·2 mins
LeetCode Easy Array Hash-Table String
LeetCode 599 note: map list1’s strings to indices, sum both indices while scanning list2, and keep every string with the minimum sum
LeetCode 804: Unique Morse Code Words
·1 min
LeetCode Easy Array Hash-Table String
LeetCode 804 note: translate each word into a Morse string and count the distinct results with a set
LeetCode 500: Keyboard Row
·1 min
LeetCode Easy Array Hash-Table String
LeetCode 500 note: make a set for each keyboard row and keep words whose lowercase character set is a subset of one row