当前位置:网站首页>力扣解法汇总953-验证外星语词典
力扣解法汇总953-验证外星语词典
2022-06-12 02:03:00 【失落夏天】
目录链接:
力扣编程题-解法汇总_分享+记录-CSDN博客
GitHub同步刷题项目:
https://github.com/September26/java-algorithms
原题链接:力扣
描述:
某种外星语也使用英文小写字母,但可能顺序 order 不同。字母表的顺序(order)是一些小写字母的排列。
给定一组用外星语书写的单词 words,以及其字母表的顺序 order,只有当给定的单词在这种外星语中按字典序排列时,返回 true;否则,返回 false。
示例 1:
输入:words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
输出:true
解释:在该语言的字母表中,'h' 位于 'l' 之前,所以单词序列是按字典序排列的。
示例 2:
输入:words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
输出:false
解释:在该语言的字母表中,'d' 位于 'l' 之后,那么 words[0] > words[1],因此单词序列不是按字典序排列的。
示例 3:
输入:words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz"
输出:false
解释:当前三个字符 "app" 匹配时,第二个字符串相对短一些,然后根据词典编纂规则 "apple" > "app",因为 'l' > '∅',其中 '∅' 是空白字符,定义为比任何其他字符都小(更多信息)。
提示:
1 <= words.length <= 100
1 <= words[i].length <= 20
order.length == 26
在 words[i] 和 order 中的所有字符都是英文小写字母。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/verifying-an-alien-dictionary
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
* 解题思路: * 把order转换为char和顺序对应关系的map,然后两两比较看是否符合字典序。 * 比较的过程中,从前往后判断,每一位判断中, * 如果index1>index2,则不符合字典序; * 如果index1<index2,则符合字典序; * 如果index1==index2且不是最后一位,则判断下一位; * 如果index1==index2且最后一位,则不符合字典序。比如apple,app。
代码:
public class Solution953 {
public boolean isAlienSorted(String[] words, String order) {
Map<Character, Integer> map = new HashMap<>();
char[] chars = order.toCharArray();
for (int i = 0; i < chars.length; i++) {
map.put(chars[i], i);
}
for (int i = 1; i < words.length; i++) {
boolean str1Bigger = isStr1Bigger(words[i - 1], words[i], map);
if (!str1Bigger) {
return false;
}
}
return true;
}
private boolean isStr1Bigger(String str1, String str2, Map<Character, Integer> map) {
char[] chars1 = str1.toCharArray();
char[] chars2 = str2.toCharArray();
for (int i = 0; i < chars1.length; i++) {
if (i >= chars2.length) {
return false;
}
int index1 = map.get(chars1[i]);
int index2 = map.get(chars2[i]);
if (index1 > index2) {
return false;
}
if (index1 < index2) {
return true;
}
continue;
}
return true;
}
}边栏推荐
- How can low code platforms improve cost effectiveness?
- [untitled]
- Advantages of Google ads
- [adjustment] in 2022, the Key Laboratory of laser life sciences of the Ministry of education of South China Normal University enrolled adjustment students in optics, electronic information, biomedicin
- php安全开放10文章列表模块的分页编写
- 2022最全面的Redis事务控制(带图讲解)
- Metaverse × How will smart cities develop?
- Basic use of MATLAB
- Data system provider Jidao technology joins dragon lizard community
- Software engineering course: Chapter 2 software problem definition and feasibility analysis after class exercises
猜你喜欢

CVPR2022 | iFS-RCNN:一种增量小样本实例分割器

RPA introduction

matplotlib. pyplot. Bar chart (II)

决定广告质量的三个主要因素

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

2022 blind box applet app has become a new drainage outlet for enterprises

如何让杀毒软件停止屏蔽某个网页?以GDATA为例

Is the bidding price fixed for each click?

自适应搜索广告有哪些优势?

2022最全面的Redis事务控制(带图讲解)
随机推荐
In 2022, the internal promotion of the "MIHA Tour" golden, silver and silver social recruitment started in April and march! Less overtime, good welfare, 200+ posts for you to choose, come and see!
Don't miss it! Five large data visualization screens that HR must collect
SQL calculates KS, AUC, IV, psi and other risk control model indicators
Summary of concrete (ground + wall) + Mountain crack data set (classification and target detection)
Lua function
redis集群(cluster)+哨兵模式+主从(replicas)
Modification of system module information of PHP security development 12 blog system
How to maximize the use of various matching methods—— Google SEM
颠倒字符串中的单词(split、双端队列)
ACL2022 | DCSR:一种面向开放域段落检索的句子感知的对比学习方法
What insurance products can you buy at the age of 60?
"China Dongxin Cup" the fourth programming competition of Guangxi University (synchronous competition)
How WPS inserts a directory and the operating steps for quickly inserting a directory
Fatal error in launcher: unable to create process using
The establishment and introduction of the announcement module of PHP development blog system
Tiobe - programming language ranking in June 2022
阿里云oss文件上传系统
ACL 2022 | 预训练语言模型和图文模型的强强联合
力扣解法汇总面试题 17.11-单词距离
Graphical data analysis | data analysis tool map