当前位置:网站首页>Force deduction solution summary - Sword finger offer II 114 Alien dictionary
Force deduction solution summary - Sword finger offer II 114 Alien dictionary
2022-06-12 02:09:00 【Lost summer】
Directory links :
Force buckle programming problem - The solution sums up _ Share + Record -CSDN Blog
GitHub Synchronous question brushing items :
https://github.com/September26/java-algorithms
Original link : Power button
describe :
There is an alien language using English letters , The alphabetical order of this language is different from that of English .
Given a list of strings words , As a dictionary of this language ,words The string in has Sorted alphabetically in the new language .
Please restore the known alphabetical order in this language according to the dictionary , and In alphabetical order array . If there is no legal alphabetical order , return "" . If there are more than one possible legal alphabetical order , Return to it Any one Order is enough .
character string s Dictionary order is less than character string t There are two situations :
At the first different letter , If s The letters in this alien language are in the alphabetical order of t Before the middle letter , that s The dictionary order of is less than t .
If the previous min(s.length, t.length) The letters are the same , that s.length < t.length when ,s The dictionary order is also less than t .
Example 1:
Input :words = ["wrt","wrf","er","ett","rftt"]
Output :"wertf"
Example 2:
Input :words = ["z","x"]
Output :"zx"
Example 3:
Input :words = ["z","x","z"]
Output :""
explain : There is no legal alphabetical order , Therefore return "" .
Tips :
1 <= words.length <= 100
1 <= words[i].length <= 100
words[i] It's only made up of lowercase letters
source : Power button (LeetCode)
link :https://leetcode.cn/problems/Jf1JuT
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Their thinking :
* Their thinking : * This difficult question , The question of time is not finished , I just thought about a general idea and wrote . * First, recursively , Find out all the dependencies . such as a stay b front , be a Of HashSet Add b. * All the dependencies that come out of this . * Then sort by Topology + Depth first search , To derive the correct string .
Code :
public class Solution114 {
// Unfinished
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);
}
}
}
}边栏推荐
- Implementation scheme of iteration and combination pattern for general tree structure
- Pagination writing of PHP security open 10 article list module
- 自适应搜索广告有哪些优势?
- “中国东信杯”广西大学第四届程序设计竞赛(同步赛)
- Summary of concrete (ground + wall) + Mountain crack data set (classification and target detection)
- Linux (centos7) installer mysql - 5.7
- 力扣解法汇总905-按奇偶排序数组
- CVPR2022 | iFS-RCNN:一种增量小样本实例分割器
- 力扣解法汇总1037-有效的回旋镖
- State owned assets into shares, has Jianye real estate stabilized?
猜你喜欢

The establishment and introduction of the announcement module of PHP development blog system

UE4\UE5触摸屏touch事件:单指、双指

Alicloud OSS file upload system

Installing MySQL version 5.5 database for Linux (centos6)

Google Ads 竞价的运作机制

Linux(CentOS6)安装MySQL5.5版本数据库

混泥土(地面+墙面)+ 山体裂缝数据集汇总(分类及目标检测)

MySQL advanced knowledge points

The release of star ring kundb 2.2 provides a new choice for business systems with high concurrent transactions and queries

阿里云oss文件上传系统
随机推荐
Add sequence number column to MySQL query result set
Graphic data analysis | data cleaning and pretreatment
"China Dongxin Cup" the fourth programming competition of Guangxi University (synchronous competition)
Force deduction solution summary 433- minimum gene change
力扣解法汇总942-增减字符串匹配
程序员应该如何解决买菜难问题?手把手带你利用无障碍辅助功能快速下单抢菜
What are the preparations for setting up Google search advertising series?
CVPR2022 | iFS-RCNN:一种增量小样本实例分割器
el-upload上传文件
Navicat for MySQL 11 Linux 破解方法
Implementation scheme of iteration and combination pattern for general tree structure
国资入股,建业地产这回稳了吗?
Force deduction solution summary 883- projected area of 3D shape
Force deduction solution summary 462- minimum number of moves to make array elements equal II
力扣解法汇总675-为高尔夫比赛砍树
力扣解法汇总1728-猫和老鼠 II
UE4\UE5触摸屏touch事件:单指、双指
力扣解法汇总905-按奇偶排序数组
Niuke monthly race 14- simple counting
力扣解法汇总868-二进制间距