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 ...
随机推荐
- JVM插码之六:jacoco插码及问题“$jacocodata 属性 Method not found: is$jacocoData”
在使用jacoco统计自动化代码覆盖率 jacoco统计自动化代码覆盖率 1. 简介1.1. 什么是JacocoJacoco是一个开源的代码覆盖率工具,可以嵌入到Ant .Maven中,并提供了Ecl ...
- Spring之28:AliasRegistry&SimpleAliasRegistry
AliasRegistry接口定义了alias的基本操作. package org.springframework.core; public interface AliasRegistry { //对 ...
- [ERROR] /D:/IDEA文件/travel/src/main/java/cn/itcast/travel/util/MailUtils.java:[1,1] 非法字符: '\ufeff'
报错: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (defau ...
- 机器学习xgboost参数解释笔记
首先xgboost有两种接口,xgboost自带API和Scikit-Learn的API,具体用法有细微的差别但不大. 在运行 XGBoost 之前, 我们必须设置三种类型的参数: (常规参数)gen ...
- java使用poi操作word, 支持动态的行(一个占位符插入多条)和表格中动态行, 支持图片
依赖 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifa ...
- ReactNative使用 react-navigation 详解
react-navigation是官方主推的导航组件,致力于解决导航卡顿,数据传递,Tabbar和navigator布局,支持redux. 最新版本为3.x,大多文档介绍的是 1.x 或 2.x 的用 ...
- nodejs之express的中间件
express中间件分成三种 内置中间件 static 自定义中间件 第三方中间件 (body-parser) (拦截器) 全局自定义中间件 在请求接口时 有几个接口都要验证传来的内容是否存在或者是否 ...
- Python opencv提取视频中的图片
作者:R语言和Python学堂链接:https://www.jianshu.com/p/e3c04d4fb5f3 这个函数就是本文要介绍的video2frames()函数,功能就是从视频中提取图片,名 ...
- stm32内联汇编
首先,先看一下mdk下的混合编程的基本方法: 使用如上方法就可以进行混合编程了. 但是要特殊注意一点,个人感觉这个是直接调用一个代码段,并非一个函数,因为他不会保护调用这个代码段之前的现场.比如: 在 ...
- 前端面试记录NO.1
后端转前端,离职后第一次面试,技术面试的时候还是比较虚的,因为基础不是很扎实.主要问了工作中用过哪些技术,主流框架的区别,jQuery的掌握情况,Ajax的掌握情况,cookie的基本内容,还有浏览器 ...