当前位置:网站首页>力扣解法汇总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;
}
}边栏推荐
- 如何最大化的利用各种匹配方式? ——Google SEM
- Google 搜索广告系列设置前有哪些准备工作?
- Redis实现消息队列的4种方案
- Ozzanation - système d'action basé sur sse
- Wechat applet - a case of comparing the size of numbers
- 2022 blind box applet app has become a new drainage outlet for enterprises
- "China Dongxin Cup" the fourth programming competition of Guangxi University (synchronous competition)
- 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!
- Huawei, this is too strong
- leetcode:6. Zigzag transformation
猜你喜欢
![[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

ozzanimation-基于sse的动作系统

Pyinstaller packaging Exe (detailed tutorial)

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

leetcodeSQL:612. Nearest distance on plane

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

ACL 2022 | 预训练语言模型和图文模型的强强联合

Bracket generation (backtracking)

商城开发知识点

Smartbi helps you solve the problem of losing high-value customers
随机推荐
Modification of system module information of PHP security development 12 blog system
Linux (centos7) installer mysql - 5.7
redis集群(cluster)+哨兵模式+主从(replicas)
RPA introduction
Redis cluster + sentinel mode + replicas
php开发 博客系统的公告模块的建立和引入
php安全开发 12博客系统的 系统模块信息的修改
ACL2022 | DCSR:一种面向开放域段落检索的句子感知的对比学习方法
CVPR2022 | iFS-RCNN:一种增量小样本实例分割器
How to automatically color cells in Excel
Operating mechanism of Google ads bidding
lua 函数
Leetcode 55 jump game
serialization and deserialization
Basic use of MATLAB
Data system provider Jidao technology joins dragon lizard community
商城开发知识点
Fatal error in launcher: unable to create process using
How to improve the advertising rating of advertising, that is, the quality score?
初探性能优化!从2个月到4小时的性能提升!