原题链接在这里:https://leetcode.com/problems/alien-dictionary/

题目:

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 non-empty words from the dictionary, where words are sorted lexicographically by the rules of this new language. Derive the order of letters in this language.

Example 1:
Given the following words in dictionary,

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

The correct order is: "wertf".

Example 2:
Given the following words in dictionary,

[
"z",
"x"
]

The correct order is: "zx".

Example 3:
Given the following words in dictionary,

[
"z",
"x",
"z"

The order is invalid, so return "".

Note:

  1. You may assume all letters are in lowercase.
  2. You may assume that if a is a prefix of b, then a must appear before b in the given dictionary.
  3. If the order is invalid, return an empty string.
  4. There may be multiple valid order of letters, return any one of them is fine.

题解:

采用BFS based topological sort. 建立graph 和 indegree array.

字典里有多个单词,这些竖着的单词是按照首字母排序的,如果首字母相同就看第二个字母,以此类推.

用queue把indegree为0的vertex加到queue中开始做BFS.

Note: when using while loop, first thing is to remember to increase index.

Time Complexity: O(V + E). Space: O(V). V最大26. Edge最大为words.length.

AC Java:

 class Solution {
public String alienOrder(String[] words) {
if(words == null || words.length == 0){
return "";
} //看看words array中都含有哪些字母
HashSet<Character> charSet = new HashSet<>();
for(String w : words){
for(char c : w.toCharArray()){
charSet.add(c);
}
} //构建 adjancy list 形式的graph, 计算每个vertex 的indegree
int [] in = new int[26];
HashMap<Character, HashSet<Character>> graph = new HashMap<>();
for(int i = 1; i < words.length; i++){
String pre = words[i - 1];
String cur = words[i];
int j = 0;
while(j < pre.length() && j < cur.length()){
if(pre.charAt(j) != cur.charAt(j)){
char sour = pre.charAt(j);
char dest = cur.charAt(j); graph.putIfAbsent(sour, new HashSet<Character>());
if(!graph.get(sour).contains(dest)){
in[dest - 'a']++;
} graph.get(sour).add(dest);
break;
} j++;
if(j < pre.length() && j == cur.length()){
return "";
}
}
} //BFS 形式的topologial sort
StringBuilder sb = new StringBuilder();
LinkedList<Character> que = new LinkedList<>();
for(char c = 'a'; c <= 'z'; c++){
if(in[c - 'a'] == 0 && charSet.contains(c)){
que.add(c);
}
} while(!que.isEmpty()){
char cur = que.poll();
sb.append(cur);
if(graph.containsKey(cur)){
for(char c : graph.get(cur)){
in[c - 'a']--;
if(in[c - 'a'] == 0){
que.add(c);
}
}
}
} //若是sb的length不等于uniqueChar的size, 说明剩下的部分有环
return sb.length() == charSet.size() ? sb.toString() : "";
}
}

类似Course Schedule.

LeetCode Alien Dictionary的更多相关文章

  1. [LeetCode] Alien Dictionary 另类字典

    There is a new alien language which uses the latin alphabet. However, the order among letters are un ...

  2. Leetcode: Alien Dictionary && Summary: Topological Sort

    There is a new alien language which uses the latin alphabet. However, the order among letters are un ...

  3. [Locked] Alien Dictionary

    Alien Dictionary There is a new alien language which uses the latin alphabet. However, the order amo ...

  4. 【Leetcode_easy】953. Verifying an Alien Dictionary

    problem 953. Verifying an Alien Dictionary solution: class Solution { public: bool isAlienSorted(vec ...

  5. Verifying an Alien Dictionary

    2019-11-24 22:11:30 953. Verifying an Alien Dictionary 问题描述: 问题求解: 这种问题有一种解法是建立新的排序和abc排序的映射,将这里的str ...

  6. [LeetCode] 269. Alien Dictionary 另类字典

    There is a new alien language which uses the latin alphabet. However, the order among letters are un ...

  7. [LeetCode] 269. Alien Dictionary 外文字典

    There is a new alien language which uses the latin alphabet. However, the order among letters are un ...

  8. LeetCode 269. Alien Dictionary

    原题链接在这里:https://leetcode.com/problems/alien-dictionary/ 题目: There is a new alien language which uses ...

  9. [leetcode]269. Alien Dictionary外星字典

    There is a new alien language which uses the latin alphabet. However, the order among letters are un ...

随机推荐

  1. BZOJ3946 : 无聊的游戏

    首先把所有串拼起来,后插入的串在前面,得到一个大串. 那么任意时刻,每个串是由这个大串的若干个不相交的子串从左到右拼接而成. 用线段树维护每个串,每个节点维护一个标记,表示区间内的串要加上什么前缀. ...

  2. 页面显示(pageshow)和页面隐藏(pagehide)事件

    Firefox和Opera有一个新特性,名叫“往返缓存”(back-forward cache,或bfcache),可以在用户使用浏览器的“后退”和“前进”按钮时加快页面的转换速度.这个缓存中不仅保存 ...

  3. BZOJ 2626 & KDtree

    题意: 二维平面n个点 每次给出一个点询问距离第k小的点. SOL: kdtree裸题,抄了一发别人的模板...二维割起来还是非常显然的.膜rzz的论文. 不多说了吧.... Code: /*==== ...

  4. Andriod phoneGap 入门

    1.下载phoneGap(我之前用还是cordova-1.5.0.jar) http://phonegap.com/download/#autodownload 解压出来,找到lib/android目 ...

  5. 运行java的class文件方法详解

    一.运行class文件 执行带main方法的class文件,命令行为:java <CLASS文件名>注意:CLASS文件名不要带文件后缀.class 例如: 复制代码代码如下: java ...

  6. Socket 与 WebSocket

    本文转载自:http://zengrong.net/post/2199.htm 1. 概述 选择了 WebSocket 技术之后,不可避免的,我要将它和其他协议以及技术做一下比较.最常见的,就是需要比 ...

  7. jquery .post .get中文参数乱码解决方法详解

    jquery默认的编码为utf-8,做项目时有时处于项目需要用到ajax提交中文参数,乱码问题就很头疼了,折腾了许久终于弄出来了.为了便于传输,我们首先将需要用到的参数用javascript自带的函数 ...

  8. MaterialCalendarView使用时遇到的问题

    一.概述 MaterialCalendarView是一个开源项目.功能强大支持多选.单选.标注等. 二.问题 1.其继承自ViewGroup,故与CalendarView半毛钱关系都没有,完全是一个新 ...

  9. [LintCode] Valid Number 验证数字

    Validate if a given string is numeric. Have you met this question in a real interview? Yes Example & ...

  10. [LintCode] Maximal Square 最大正方形

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...