Apple | Phone | 269. Alien Dictionary | 2020

Difficulty: MEDIUMCategory: InterviewExperienceFrequency: 1Asked On 2020-04-13Added By Anonymous
Source: Link

I just have an phone interview with Apple, but I did not really good explanation for this question. Therefore, I want to "re-eat" this question. Upvote my solution if you like it: https://leetcode.com/problems/alien-dictionary/discuss/577291/python-easy-topological-sort-solution


First of all, for this kind of question, you should know it belongs to Topological Sort. Why? 1) you need to solve it based on the direct graph, 2) it has ordered for each vertex in the graph. Thus, once you find information like need graph and exist order in question description. Then, you need to consider from Topological Sort. Next, use your Topological Sort templet to solve it: 1) build a directed graph with degree 2) use BFS to prune graph from low degree vertex to high degree vertex. 3) check if it is the acyclic directed graph.


In LeetCode, there is one new test case "["abc","ab"]" that I use my templete cannot pass it. Therefore, I add one condition to hack it because this is not vaild input, I should return "":

1 if not value_graph and len(words) == 2 and len(words[0]) > len(words[1]): 2 3 return res

Same question: https://leetcode.com/problems/course-schedule-ii/ and https://leetcode.com/problems/course-schedule/


Time complexity: O(v+e)


Space complexity: O(v)

1import collections 2 3class Solution(object): 4 def alienOrder(self, words): 5 """ 6 :type words: List[str] 7 :rtype: str 8 """ 9 10 data_value_dict = set(''.join(words)) 11 degree_value_dict = {x: 0 for x in data_value_dict} 12 value_graph = collections.defaultdict(list) 13 value_list = collections.deque() 14 res = '' 15 16 for pair in zip(words, words[1:]): 17 for x, y in zip(*pair): 18 if x != y: 19 value_graph[x].append(y) 20 degree_value_dict[y] += 1 21 break 22 23 if not value_graph and len(words) == 2 and len(words[0]) > len(words[1]): 24 25 return res 26 27 for key, value in degree_value_dict.items(): 28 if not value: 29 value_list.append(key) 30 31 while value_list: 32 temp_value = len(value_list) 33 34 for _ in range(temp_value): 35 a = value_list.popleft() 36 res += a 37 38 for b in value_graph[a]: 39 degree_value_dict[b] -= 1 40 41 if not degree_value_dict[b]: 42 value_list.append(b) 43 44 if set(res) != data_value_dict: 45 res = '' 46 47 return res


I also post my 207. Course Schedule solution, so you can see I use templete to solve both questions.

1import collections 2 3class Solution(object): 4 def canFinish(self, numCourses, prerequisites): 5 """ 6 :type numCourses: int 7 :type prerequisites: List[List[int]] 8 :rtype: bool 9 """ 10 11 value_graph = collections.defaultdict(list) 12 degree_value_list = [0] * numCourses 13 value_list = collections.deque() 14 count = 0 15 res = False 16 17 for a, b in prerequisites: 18 value_graph[a].append(b) 19 degree_value_list[b] += 1 20 21 for i in range(numCourses): 22 if degree_value_list[i] == 0: 23 value_list.append(i) 24 25 while value_list: 26 temp_value = len(value_list) 27 28 for _ in range(temp_value): 29 a = value_list.popleft() 30 count += 1 31 32 for b in value_graph[a]: 33 degree_value_list[b] -= 1 34 35 if not degree_value_list[b]: 36 value_list.append(b) 37 38 if count == numCourses: 39 res = True 40 41 return res
Comments