[LeetCode] Alien Dictionary

简介: Problem Description: There is a new alien language which uses the latin alphabet. However, the order among letters are unknown to you.

Problem Description:

There is a new alien language which uses the latin alphabet. However, the order among letters are unknown to you. You receive a list of words from the dictionary, wherewords are sorted lexicographically by the rules of this new language. Derive the order of letters in this language.

For example,
Given the following words in dictionary,

[
  "wrt",
  "wrf",
  "er",
  "ett",
  "rftt"
]

The correct order is: "wertf".

Note:

    1. You may assume all letters are in lowercase.
    2. If the order is invalid, return an empty string.
    3. There may be multiple valid order of letters, return any one of them is fine.

Well, this problem is not that easy. First you may need some clarifications about the problem itself. If you do, you may refer to this post for a nice example which illustrates the purpose of this problem.

Moreover, you need to understand graph representation, graph traversal and specifically, topological sort, which are all needed to solve this problem cleanly.


DFS

Fortunately, jaewoo posts a nice solution in this post, whose code is rewritten as follows by decomposing the code into two parts:

  1. make_graph: Build the graph as a list of adjacency lists;
  2. toposort and acyclic: Traverse the graph in DFS manner to check for cycle and generate the topological sort.
 1 class Solution {
 2 public:
 3     string alienOrder(vector<string>& words) {
 4         if (words.size() == 1) return words[0];
 5         graph g = make_graph(words);
 6         return toposort(g);
 7     }
 8 private:
 9     typedef unordered_map<char, unordered_set<char>> graph;
10     
11     graph make_graph(vector<string>& words) {
12         graph g;
13         int n = words.size();
14         for (int i = 1; i < n; i++) {
15             bool found = false;
16             string word1 = words[i - 1], word2 = words[i];
17             int m = word1.length(), n = word2.length(), l = max(m, n);
18             for (int j = 0; j < l; j++) {
19                 if (j < m && g.find(word1[j]) == g.end())
20                     g[word1[j]] = unordered_set<char>();
21                 if (j < n && g.find(word2[j]) == g.end())
22                     g[word2[j]] = unordered_set<char>();
23                 if (j < m && j < n && word1[j] != word2[j] && !found) {
24                     g[word1[j]].insert(word2[j]);
25                     found = true;
26                 }
27             }
28         }
29         return g;
30     }
31     
32     string toposort(graph& g) {
33         vector<bool> path(256, false), visited(256, false);
34         string topo;
35         for (auto adj : g)
36             if (!acyclic(g, path, visited, topo, adj.first))
37                 return "";
38         reverse(topo.begin(), topo.end());
39         return topo;
40     }
41     
42     bool acyclic(graph& g, vector<bool>& path, vector<bool>& visited, string& topo, char node) {
43         if (path[node]) return false;
44         if (visited[node]) return true;
45         path[node] = visited[node] = true;
46         for (auto neigh : g[node])
47             if (!acyclic(g, path, visited, topo, neigh))
48                 return false;
49         path[node] = false;
50         topo += node;
51         return true;
52     }
53 };

BFS

Well, given the graph well represented, the BFS solution is also not that hard :-)

 1 class Solution {
 2 public:
 3     string alienOrder(vector<string>& words) {
 4         if (words.size() == 1) return words[0];
 5         graph g = make_graph(words);
 6         unordered_map<char, int> degrees = compute_indegree(g);
 7         int numNodes = degrees.size();
 8         string order;
 9         queue<char> toVisit;
10         for (auto node : degrees)
11             if (!node.second)
12                 toVisit.push(node.first);
13         for (int i = 0; i < numNodes; i++) {
14             if (toVisit.empty()) return "";
15             char c = toVisit.front();
16             toVisit.pop();
17             order += c;
18             for (char neigh : g[c])
19                 if (!--degrees[neigh])
20                     toVisit.push(neigh);
21         }
22         return order;
23     }
24 private:
25     typedef unordered_map<char, unordered_set<char>> graph;
26 
27     graph make_graph(vector<string>& words) {
28         graph g;
29         int n = words.size();
30         for (int i = 1; i < n; i++) {
31             bool found = false;
32             string word1 = words[i - 1], word2 = words[i];
33             int l1 = word1.length(), l2 = word2.length(), l = max(l1, l2);
34             for (int j = 0; j < l; j++) {
35                 if (j < l1 && g.find(word1[j]) == g.end())
36                     g[word1[j]] = unordered_set<char>();
37                 if (j < l2 && g.find(word2[j]) == g.end())
38                     g[word2[j]] = unordered_set<char>();
39                 if (j < l1 && j < l2 && word1[j] != word2[j] && !found) {
40                     g[word1[j]].insert(word2[j]);
41                     found = true;
42                 }
43             }
44         }
45         return g; 
46     }
47 
48     unordered_map<char, int> compute_indegree(graph& g) {
49         unordered_map<char, int> degrees;
50         for (auto adj : g) {
51             if (!degrees[adj.first]);
52             for (char neigh : adj.second)
53                 degrees[neigh]++;
54         }
55         return degrees;
56     }
57 };

BTW, if (!degrees[adj.first]); in compute_indegree is to make sure that a node with 0indegree will not be left out. For this part, something about the default constructor ofunordered_map is useful: each time when we try to access a key k which is still not inunordered_map by [k], the default constructor of unordered_map will set its value to 0.


Well, this problem is not easy and the code is also long. If you have difficulties understanding it,  you may review the fundamentals of graph (Introduction to Algorithms is a good reference) and try to solve older LeetCode problems like Clone Graph, Course Schedule and Course Schedule II first.

目录
相关文章
Leetcode-Easy 953. Verifying an Alien Dictionary
Leetcode-Easy 953. Verifying an Alien Dictionary
124 0
|
2月前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
3月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
57 6
|
3月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
114 2
|
20天前
|
机器学习/深度学习 人工智能 自然语言处理
280页PDF,全方位评估OpenAI o1,Leetcode刷题准确率竟这么高
【10月更文挑战第24天】近年来,OpenAI的o1模型在大型语言模型(LLMs)中脱颖而出,展现出卓越的推理能力和知识整合能力。基于Transformer架构,o1模型采用了链式思维和强化学习等先进技术,显著提升了其在编程竞赛、医学影像报告生成、数学问题解决、自然语言推理和芯片设计等领域的表现。本文将全面评估o1模型的性能及其对AI研究和应用的潜在影响。
16 1
|
2月前
|
数据采集 负载均衡 安全
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
本文提供了多个多线程编程问题的解决方案,包括设计有限阻塞队列、多线程网页爬虫、红绿灯路口等,每个问题都给出了至少一种实现方法,涵盖了互斥锁、条件变量、信号量等线程同步机制的使用。
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口