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 ...
随机推荐
- todo---callback
todo---callback https://blog.csdn.net/u010158267/article/details/51426963/
- fputcsv 导出excel,解决内存、性能、乱码、科学计数法问题
在PHP的日常开发中,时常会需要导出 excel ,一般我们会使用 PHPExcel ,性能强大,但是在数据量大的时候,phpexcel 性能差.内存溢出等各种不可控问题就会出现.因此,如果对导出样式 ...
- S04_CH02_工程移植ubuntu并一键制作启动盘
S04_CH02_工程移植ubuntu并一键制作启动盘 2.1概述 2.2搭建硬件系统 本章硬件工程还是使用<S04_CH01_搭建工程移植LINUX/测试EMMC/VGA>所搭建的VIV ...
- Dockerfile编写,以及设置一个自启动脚本
FROM:指定基础镜像,必须为第一个命令 MAINTAINER: 维护者信息 RUN:构建镜像时执行的命令 ADD:将本地文件添加到容器中,tar类型文件会自动解压(网络压缩资源不会被解压),可以访问 ...
- C#使用管理员权限打开cmd执行命令行
最近遇到个棘手的问题,服务器远程连不上,但是ftp可以,可能远程连接的服务挂了或者防火墙入站规则有点问题,想要重启,得找机房工作人员,还是挺麻烦的 想了想可以上传个执行cmd命令的东西,然后远程访问触 ...
- dfs的剪枝优化
两个剪枝问题 1. 当两点的距离(需要走的步数)大于剩下的时间时 剪去 2.奇偶剪枝问题 如果起点到终点所需走的步数的奇偶性与时间奇偶性不同的时候 剪去 起点到终点步数的奇偶性的判断 首先 明确点的奇 ...
- Mac下Sublime Text3激活码
方法1: 终端中打开文件 /etc/hosts,插入如下语句 127.0.0.1 www.sublimetext.com 127.0.0.1 license.sublimehq.com 方法2: 在s ...
- Java Comparable与Comparator区别
1,两种接口的展示 下面的程序是两个类各自实现了Comparable接口.Comparator接口 package com.cnblogs.mufasa.Solution; import java.u ...
- JVM性能优化--类加载器,手动实现类的热加载
一.类加载的机制的层次结构 每个编写的".java"拓展名类文件都存储着需要执行的程序逻辑,这些".java"文件经过Java编译器编译成拓展名为". ...
- 虚拟机安装master,克隆slave0、slave1(多台机相连,网络匹配)
1.在虚拟机中创建一个名叫master的主机 2.创建成功后,打开编辑选项——虚拟网络网络编辑器,填网关 3.打开终端,进入root权限,编写命令 设置虚拟机DNS 4.给master网络配置好后,克 ...