LeetCode 269. Alien Dictionary
原题链接在这里: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:
- You may assume all letters are in lowercase.
- You may assume that if a is a prefix of b, then a must appear before b in the given dictionary.
- If the order is invalid, return an empty string.
- 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() : "";
}
}
LeetCode 269. Alien Dictionary的更多相关文章
- [LeetCode] 269. Alien Dictionary 另类字典
There is a new alien language which uses the latin alphabet. However, the order among letters are un ...
- [LeetCode] 269. Alien Dictionary 外文字典
There is a new alien language which uses the latin alphabet. However, the order among letters are un ...
- [leetcode]269. Alien Dictionary外星字典
There is a new alien language which uses the latin alphabet. However, the order among letters are un ...
- 269. Alien Dictionary
题目: There is a new alien language which uses the latin alphabet. However, the order among letters ar ...
- 269. Alien Dictionary 另类字典 *HARD*
There is a new alien language which uses the latin alphabet. However, the order among letters are un ...
- 269. Alien Dictionary火星语字典(拓扑排序)
[抄题]: There is a new alien language which uses the latin alphabet. However, the order among letters ...
- [Locked] Alien Dictionary
Alien Dictionary There is a new alien language which uses the latin alphabet. However, the order amo ...
- 【Leetcode_easy】953. Verifying an Alien Dictionary
problem 953. Verifying an Alien Dictionary solution: class Solution { public: bool isAlienSorted(vec ...
- Verifying an Alien Dictionary
2019-11-24 22:11:30 953. Verifying an Alien Dictionary 问题描述: 问题求解: 这种问题有一种解法是建立新的排序和abc排序的映射,将这里的str ...
随机推荐
- [转帖]crontab每小时运行一次
crontab每小时运行一次 先给出crontab的语法格式 对于网上很多给出的每小时定时任务写法,可以说绝大多数都是错误的!比如对于下面的这种写法: 00 * * * * #每隔一小时执行一 ...
- Android--文件存取
import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundExc ...
- 【字符串hash】DNA
DNA 题目描述 小X身为奆老,兴趣爱好广泛,他还非常喜欢研究DNA序列……小X进行了一项关于DNA序列研究,发现人某条染色体上的一段DNA序列中连续的k个碱基组成的碱基序列与做题的AC率有关!于是他 ...
- SPA中使用jwt
什么是jwt? JSON Web Token (JWT),它是目前最流行的跨域身份验证解决方案 JWT的工作原理 1. 是在服务器身份验证之后,将生成一个JSON对象并将其发送回用户,示例如下:{&q ...
- [The 2019 Asia Yinchuan First Round Online Programming] D Take Your Seat
也许更好的阅读体验 \(\mathcal{Description}\) 原题链接 题目大意 该题目有两个问题 \(Task\ 1\),有\(n\)个人\(n\)个座位,每个人都有一个对应的座位,每个人 ...
- MQ与logstash实现ES与数据库同步区别
Logstash 实现ES 与数据库同步: 使用定时器(使用sql 定时的去查询数据进行同步).实现方式比较简单. MQ 实现 ES 与数据库同步: 实时性,消息放到MQ中,消费者会自动的消费,复杂性 ...
- 第2章 NIO入门
2.1 传统的BIO编程 以服务器为例,在传统BIO模型下的服务器,每当一个新的请求到来的时候回分配一个线程去处理该请求,并且该线程在执行IO操作的时候会一直阻塞,知道IO操作完成或抛出异常才会返回. ...
- 如何录屏做GIF图
网上找了一下,ScreenToGif 这个神器 https://github.com/NickeManarin/ScreenToGif https://github.com/NickeManarin/ ...
- cell上的按钮点击和左滑冲突
cell上的某个按钮的点击事件,当cell左滑的时候,只要活动的区域也在按钮上,那么按钮的点击事件也会调用. fix: 给按钮添加一个手势(TapGesture)那么当点击的时候就会响应点击手势的方法 ...
- py-1 语言介绍
一.编程与编程语言 1.编程的目的 计算机的发明,是为了用机器取代并解放人力.而编程的目的则是将人类的思想流程按照某种能够被计算机识别的表达方式传递给计算机,从而达到让计算机能够像人脑.电脑一样自动执 ...