当前位置:网站首页>力扣解法汇总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;
}
}边栏推荐
- Implementation scheme of iteration and combination pattern for general tree structure
- Leetcode 45 jump game II
- 国资入股,建业地产这回稳了吗?
- 括号生成(回溯)
- What are the advantages of adaptive search advertising?
- LeetCode Algorithm 1791. Find the central node of the star chart
- 关于vagrant up的一个终结之谜
- C asynchronous programming from simple to deep (III) details awaiter
- 力扣解法汇总1037-有效的回旋镖
- 混泥土(地面+墙面)+ 山体裂缝数据集汇总(分类及目标检测)
猜你喜欢

初探性能优化!从2个月到4小时的性能提升!
![[adjustment] notice on the opening of the 2022 pre adjustment system for postgraduate enrollment of Shanghai Second University of Technology](/img/16/2f9a235995cdd54ac9b85a68a7afcb.jpg)
[adjustment] notice on the opening of the 2022 pre adjustment system for postgraduate enrollment of Shanghai Second University of Technology

Is there a female Bluetooth headset suitable for girls? 38 Bluetooth headsets worth getting started

On the night of the joint commissioning, I beat up my colleagues

Summary of concrete (ground + wall) + Mountain crack data set (classification and target detection)

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

Why do we use Google search ads?

Don't miss it! Five large data visualization screens that HR must collect

RPA introduction

How to stop anti-virus software from blocking a web page? Take gdata as an example
随机推荐
[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
Redis實現消息隊列的4種方案
如何最大化的利用各种匹配方式? ——Google SEM
ACL2022 | DCSR:一种面向开放域段落检索的句子感知的对比学习方法
Redis cluster + sentinel mode + replicas
Comprehensive quality of teaching resources in the second half of 2019 - subjective questions
Design principle [Demeter's Law]
What are the advantages of adaptive search advertising?
Wide match modifier symbol has been deprecated, do not use
Leetcode 1005 maximized array sum after K negations
聯調這夜,我把同事打了...
virsh创建/关闭/停止虚拟机常用的几条指令
入手Ticwatch2
Three main factors determining advertising quality
力扣解法汇总497-非重叠矩形中的随机点
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!
Pyinstaller packaging Exe (detailed tutorial)
广泛匹配修饰符符号已经被弃用,请勿使用
打包一个包含手表端应用的手机端APK应用—Ticwear
[learn FPGA programming from scratch -19]: quick start chapter - operation steps 4-1- Verilog software download and construction of development environment - Altera quartz II version