← Home

953. Verifying an Alien Dictionary

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
953

I like to read this solution as a small machine: keep the useful information, throw away the noise. For 953. Verifying an Alien Dictionary, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are c2i, isAlienSorted.

Guide

Why?

The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Initialize the memory or helper structure.
  2. Process candidates in the order the invariant expects.
  3. Update the answer only when the current state is valid.
  4. Return the value that represents the fully processed input.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(C), where C is the total content of words.
  • Space: O(1).

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01/**
02In 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.
03
04Given 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 lexicographicaly in this alien language.
05
06 
07
08Example 1:
09
10Input: words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
11Output: true
12Explanation: As 'h' comes before 'l' in this language, then the sequence is sorted.
13Example 2:
14
15Input: words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
16Output: false
17Explanation: As 'd' comes after 'l' in this language, then words[0] > words[1], hence the sequence is unsorted.
18Example 3:
19
20Input: words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz"
21Output: false
22Explanation: The first three characters "app" match, and the second string is shorter (in size.) According to lexicographical rules "apple" > "app", because 'l' > '∅', where '∅' is defined as the blank character which is less than any other character (More info).
23 
24
25Note:
26
271 <= words.length <= 100
281 <= words[i].length <= 20
29order.length == 26
30All characters in words[i] and order are english lowercase letters.
31**/
32
33/**
34Complexity Analysis
35Time Complexity: O(C), where C is the total content of words.
36Space Complexity: O(1). 
37**/
38
39//Runtime: 8 ms, faster than 99.47% of C++ online submissions for Verifying an Alien Dictionary.
40//Memory Usage: 9.8 MB, less than 45.54% of C++ online submissions for Verifying an Alien Dictionary.
41class Solution {
42public:
43    int c2i(char c, string& order){
44        return find(order.begin(), order.end(), c) - order.begin();
45    }
46    bool isAlienSorted(vector<string>& words, string order) {
47        for(int i = 0; i < words.size() - 1; i++){
48            int l = min(words[i].size(), words[i+1].size());
49            bool formerSame = true;
50            for(int j = 0; j < l; j++){
51                if(c2i(words[i][j], order) < c2i(words[i+1][j], order)){
52                    formerSame = false;
53                    break;
54                }else if(c2i(words[i][j], order) > c2i(words[i+1][j], order)){
55                    return false;
56                }
57            }
58            if(formerSame && words[i].size() > words[i+1].size()){
59                return false;
60            }
61        }
62        return true;
63    }
64};

Cost

Complexity

Time
O(C), where C is the total content of words.
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(1).
Auxiliary state plus the answer structure where the problem requires one.