Advertisement

Verifying an Alien Dictionary - LeetCode 953 Solution

Verifying an Alien Dictionary - Complete Solution Guide

Verifying an Alien Dictionary is LeetCode problem 953, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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. Example 1: Input: words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz" Output: true Explanation: As 'h' comes before 'l'

Detailed Explanation

The problem asks us to determine if a given list of words (`words`) is sorted according to a custom alien alphabet (`order`). The `order` string defines the order of the lowercase English letters. We need to compare the words in the `words` list lexicographically, using the alien alphabet defined by `order`, and return `true` if the words are sorted correctly, and `false` otherwise. The words only contain lowercase English letters, and the order string is a permutation of all 26 lowercase letters.

Solution Approach

The solution involves creating a mapping (using a dictionary or array) from each character in the `order` string to its index. This mapping represents the alien alphabet's ordering. Then, we iterate through the `words` list, comparing adjacent pairs of words using the alien alphabet's ordering. If any adjacent pair is out of order, we return `false`. If we reach the end of the list without finding any out-of-order pairs, we return `true`.

Step-by-Step Algorithm

  1. Step 1: Create a map (dictionary or array) called `order_map` that stores the index of each character in the `order` string. For example, if `order` is "hlabcdefgijkmnopqrstuvwxyz", then `order_map['h']` will be 0, `order_map['l']` will be 1, `order_map['a']` will be 2, and so on.
  2. Step 2: Iterate through the `words` list from index 0 to `len(words) - 2` (inclusive), comparing each word with the next word.
  3. Step 3: For each pair of words (word1, word2), compare their characters one by one until you find a difference or reach the end of one of the words.
  4. Step 4: If a character difference is found, compare their indices in `order_map`. If `order_map[word1[i]] > order_map[word2[i]]`, then word1 comes after word2, so the words are out of order. Return `false`.
  5. Step 5: If one word is a prefix of the other (i.e., all characters are the same up to the length of the shorter word), the shorter word must come first. If word1 is longer than word2, the words are out of order. Return `false`.
  6. Step 6: If the loop completes without finding any out-of-order pairs, the words are sorted according to the alien alphabet. Return `true`.

Key Insights

  • Insight 1: The core idea is to create a mapping between the alien alphabet and the standard alphabet to easily compare characters. Using a dictionary or array to store the index of each letter in the `order` string is crucial.
  • Insight 2: Comparing two words involves iterating through their characters until a difference is found or one word runs out of characters. If one word is a prefix of another, the shorter word must come first in lexicographical order.
  • Insight 3: The most efficient solution checks the order of adjacent word pairs, returning immediately if an unsorted pair is encountered.

Complexity Analysis

Time Complexity: O(N*M)

Space Complexity: O(1)

Topics

This problem involves: Array, Hash Table, String.

Companies

Asked at: Snap, Wix.