当前位置:网站首页>力扣解法汇总-剑指 Offer II 114. 外星文字典
力扣解法汇总-剑指 Offer II 114. 外星文字典
2022-06-12 02:03:00 【失落夏天】
目录链接:
力扣编程题-解法汇总_分享+记录-CSDN博客
GitHub同步刷题项目:
https://github.com/September26/java-algorithms
原题链接:力扣
描述:
现有一种使用英语字母的外星文语言,这门语言的字母顺序与英语顺序不同。
给定一个字符串列表 words ,作为这门语言的词典,words 中的字符串已经 按这门新语言的字母顺序进行了排序 。
请你根据该词典还原出此语言中已知的字母顺序,并 按字母递增顺序 排列。若不存在合法字母顺序,返回 "" 。若存在多种可能的合法字母顺序,返回其中 任意一种 顺序即可。
字符串 s 字典顺序小于 字符串 t 有两种情况:
在第一个不同字母处,如果 s 中的字母在这门外星语言的字母顺序中位于 t 中字母之前,那么 s 的字典顺序小于 t 。
如果前面 min(s.length, t.length) 字母都相同,那么 s.length < t.length 时,s 的字典顺序也小于 t 。
示例 1:
输入:words = ["wrt","wrf","er","ett","rftt"]
输出:"wertf"
示例 2:
输入:words = ["z","x"]
输出:"zx"
示例 3:
输入:words = ["z","x","z"]
输出:""
解释:不存在合法字母顺序,因此返回 "" 。
提示:
1 <= words.length <= 100
1 <= words[i].length <= 100
words[i] 仅由小写英文字母组成
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/Jf1JuT
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
* 解题思路: * 这题困难题,时间问题没有完成,只是想了个大体思路写了一下。 * 先通过递归的方式,找出其所有的依赖关系。比如a在b前面,则a的HashSet中添加b。 * 这样最终得出的所有的依赖。 * 然后通过拓扑排序 + 深度优先搜索进行搜索,从而推导出正确的字符串。
代码:
public class Solution114 {
//未完成的
public String alienOrder(String[] words) {
HashSet<Character>[] sets = new HashSet[26];
for (int i = 0; i < 26; i++) {
sets[i] = new HashSet<>();
}
searchRely(0, Arrays.asList(words), sets);
return "";
}
private void searchRely(int index, List<String> words, HashSet<Character>[] sets) {
if (words.size() == 0) {
return;
}
char lastChar = 0;
ArrayList<String> newList = new ArrayList<>();
for (int i = 0; i <= words.size(); i++) {
if (i == words.size()) {
searchRely(index + 1, newList, sets);
break;
}
String str = words.get(i);
char c = str.charAt(index);
if (lastChar == 0) {
lastChar = c;
newList.add(str);
continue;
}
if (c != lastChar) {
sets[c - 'a'].add(lastChar);
lastChar = c;
searchRely(index + 1, newList, sets);
newList.clear();
} else {
newList.add(str);
}
}
}
}边栏推荐
- matplotlib. pyplot. Bar chart (II)
- Metaverse × How will smart cities develop?
- 商城开发知识点
- Don't miss it! Five large data visualization screens that HR must collect
- [untitled]
- 决定广告质量的三个主要因素
- Software engineering - system flow chart
- Explore performance optimization! Performance improvement from 2 months to 4 hours!
- adb命令 利用jks文件给apk签名
- 关于vagrant up的一个终结之谜
猜你喜欢

Subject knowledge and educational ability of information technology in the first half of 2019 – subjective questions

Google Ads 竞价的运作机制

初探性能优化!从2个月到4小时的性能提升!

Almost all schools will ask for the second round exam! Come in and recite the answer!

Pyinstaller packaging Exe (detailed tutorial)

Smartbi helps you solve the problem of losing high-value customers

Redis实现消息队列的4种方案

The most comprehensive redis transaction control in 2022 (with illustration)

Three main factors determining advertising quality

通用树形结构的迭代与组合模式实现方案
随机推荐
颠倒字符串中的单词(split、双端队列)
UE4\UE5触摸屏touch事件:单指、双指
Websocket is closed after 10 seconds of background switching
CVPR2022 | iFS-RCNN:一种增量小样本实例分割器
Graphical data analysis | business analysis and data mining
Redis集群更换节点IP后如何恢复集群并保留完整集群数据
Fatal error in launcher: unable to create process using
Linux(CentOS7)安装MySQL-5.7版本
How to improve the advertising rating of advertising, that is, the quality score?
Linux(CentOS6)安装MySQL5.5版本数据库
[untitled]
如何为Excel中的单元格自动填充颜色
leetcodeSQL:612. Nearest distance on plane
The most comprehensive redis transaction control in 2022 (with illustration)
如何提高广告的广告评级,也就是质量得分?
Software engineering - system flow chart
2022 blind box applet app has become a new drainage outlet for enterprises
Almost all schools will ask for the second round exam! Come in and recite the answer!
[C language] C language file operation | C language file reading and writing | single character reading and writing | string reading and writing | format reading and writing | binary form reading and
PyGame alien invasion