953. Verifying an Alien Dictionary

Verifying an Alien Dictionary (LeetCode 953) is a Easy Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Array/Matrix Manipulation Patterns pattern, in the Hashing - Frequency Map / Counting subpattern.

How to solve Verifying an Alien Dictionary

Verifying an Alien Dictionary is a Hashing Frequency Map and Counting problem, part of Array/Matrix Manipulation Patterns. Master hash map frequency counting for array problems. Count occurrences, find duplicates, and solve majority element problems efficiently.

Work towards the solution in these steps:

  1. Create a rank for each lowercase letter based on its position in `order`.
  2. It is sufficient to check each word against the word immediately after it.
  3. Scan a pair from left to right until the first mismatch, or until one word ends.

The reference solution runs in O(sum of the lengths of the words) time and O(1) space.

The Verifying an Alien Dictionary solution and editorial walks through the full approach with reference code.

Problem statement

Implement Solution.isAlienSorted(words, order) to determine whether the list words is sorted according to an alien alphabet. The string order gives the complete character ranking: a character appearing earlier in order is considered smaller than one appearing later.

Compare words lexicographically using this ranking. For two words, the first position where their characters differ determines their order. If one word is a prefix of the other, the shorter word must come first. Equal words are allowed.

Return true if every consecutive pair in words follows these rules; otherwise, return false.

Example 1:

Input: words = ["mango","map","mat"], order = "abcdefghijklmnopqrstuvwxyz" Output: true Explanation: The words are in ordinary alphabetical order: "mango" comes before "map", and "map" comes before "mat" at their first differing character.

Example 2:

Input: words = ["zone","zoo"], order = "zyxwvutsrqponmlkjihgfedcba" Output: false Explanation: After the shared prefix z, the reversed alphabet ranks o before n, so zoo should come before zone.

Example 3:

Input: words = ["cabin","cabinet"], order = "abcdefghijklmnopqrstuvwxyz" Output: true Explanation: cabin is a prefix of cabinet, and the shorter word correctly appears first.

Constraints:

- 1 <= words.length <= 100 - 1 <= words[i].length <= 20 - words[i] contains only lowercase English letters - order.length = 26 - order is a permutation of the 26 lowercase English letters

Topics and companies

Topics: String, Hash Table, Array, Two Pointers.

How to practise Verifying an Alien Dictionary on Thita.ai

Starter code is provided in C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(sum of the lengths of the words) time and O(1) space.

Open Verifying an Alien Dictionary in the code editor, or read the Verifying an Alien Dictionary editorial for a worked solution with its approach and complexity analysis.

Where Verifying an Alien Dictionary sits in the DSA pattern sheet

Problems related to Verifying an Alien Dictionary

Other problems that use the same Hashing - Frequency Map / Counting and Array/Matrix Manipulation Patterns techniques:

Verifying an Alien Dictionary FAQ

What is the LeetCode number of Verifying an Alien Dictionary?

Verifying an Alien Dictionary is LeetCode problem 953, rated Easy.

What pattern is used to solve Verifying an Alien Dictionary?

Verifying an Alien Dictionary is a Hashing Frequency Map and Counting problem. It belongs to the Array/Matrix Manipulation Patterns pattern, in the Hashing - Frequency Map / Counting subpattern.

What is the time complexity of Verifying an Alien Dictionary?

The reference solution runs in O(sum of the lengths of the words) time and O(1) space.

Preparing this workspace.