The Alien Dictionary Verification Algorithm

  • 时间:2020-09-30 16:23:25
  • 分类:网络文摘
  • 阅读:79 次

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 lexicographicaly in this alien language.

Example 1:
Input: words = [“hello”,”leetcode”], order = “hlabcdefgijkmnopqrstuvwxyz”
Output: true
Explanation: As ‘h’ comes before ‘l’ in this language, then the sequence is sorted.

Example 2:

Input: words = [“word”,”world”,”row”], order = “worldabcefghijkmnpqstuvxyz”
Output: false
Explanation: As ‘d’ comes after ‘l’ in this language, then words[0] > words[1], hence the sequence is unsorted.

Example 3:
Input: words = [“apple”,”app”], order = “abcdefghijklmnopqrstuvwxyz”
Output: false
Explanation: 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).

Note:
1 <= words.length <= 100
1 <= words[i].length <= 20
order.length == 26
All characters in words[i] and order are english lowercase letters.

How to verify the alien dictionary in C++?

The first step is to process the input dictionary order, where we can record the index of the lowercase letters i.e. in a static array of size 26.

Then, we need to design a function to compare two words (strings) based on this new alien dictionary. We compare characters by characters – base on two criterias the dictionary and the string length (a shorter string comes before a longer one e.g. abc comes before abcd.

The overall complexity is O(MN) where N is the number of words in the dictionary and the M is the average length of the word in the alien dictionary.

The neighbour words are compared and once out of order, simply return false. Otherwise, return true in the worst cases when the list of words are in order.

The C++ algorithm to check if a list of words are alien-sorted is given below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
public:
    bool isAlienSorted(vector<string>& words, string order) {
        int table[26];
        // remember the letter order/index
        for (int i = 0; i < order.size(); ++ i) {
            table[order[i] - 97] = i;
        }
        // compare the neighbour words.
        for (int i = 0; i < words.size() - 1; ++ i) {
            if (!inorder(words[i], words[i + 1], table)) {
                return false;
            }
        }
        return true;
    }
private: // check if two words are in alien order based on the alien dictionary.
    bool inorder(string a, string b, int table[]) {
        for (int i = 0; i < min(a.size(), b.size()); ++ i) {
            if (table[a[i] - 97] > table[b[i] - 97]) {
                return false;
            } else if (table[a[i] - 97] < table[b[i] - 97]) {
                return true;
            }
        }
        return a.size() < b.size(); // shorter string comes first.
    }
};
class Solution {
public:
    bool isAlienSorted(vector<string>& words, string order) {
        int table[26];
        // remember the letter order/index
        for (int i = 0; i < order.size(); ++ i) {
            table[order[i] - 97] = i;
        }
        // compare the neighbour words.
        for (int i = 0; i < words.size() - 1; ++ i) {
            if (!inorder(words[i], words[i + 1], table)) {
                return false;
            }
        }
        return true;
    }
private: // check if two words are in alien order based on the alien dictionary.
    bool inorder(string a, string b, int table[]) {
        for (int i = 0; i < min(a.size(), b.size()); ++ i) {
            if (table[a[i] - 97] > table[b[i] - 97]) {
                return false;
            } else if (table[a[i] - 97] < table[b[i] - 97]) {
                return true;
            }
        }
        return a.size() < b.size(); // shorter string comes first.
    }
};

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
做优化的几个方法,SEO优化必备技巧  网站遭遇负面SEO怎么办  为什么大部分设计师和网站都对蓝色偏爱有加  网络安全公司实习生的经验分享  运营笔记:是时候了解蜘蛛爬取原理了!揭秘收录难题  新网站关键词怎么做优化会更好?  怎么给网站优化?切忌做标题党  运营笔记:SEO快排那些事儿!  运营笔记:你的网站为什么不收录?看看这篇文章的解读!  数学题:甲乙两人分别从AB两点出发 
评论列表
添加评论