当前位置:网站首页>Force deduction solution summary 953 verification of alien language dictionary
Force deduction solution summary 953 verification of alien language dictionary
2022-06-12 02:08: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 :
Some alien languages also use small letters in English , But it may be in order order Different . The order of the alphabet (order) It's an arrangement of small letters .
Given a set of words written in alien language words, And the order of its alphabet order, Only when given words are arranged in dictionary order in this alien language , return true; otherwise , return false.
Example 1:
Input :words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
Output :true
explain : In the alphabet of the language ,'h' be located 'l' Before , So the word sequence is arranged in dictionary order .
Example 2:
Input :words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
Output :false
explain : In the alphabet of the language ,'d' be located 'l' after , that words[0] > words[1], Therefore, the word sequence is not arranged in dictionary order .
Example 3:
Input :words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz"
Output :false
explain : Current three characters "app" When the match , The second string is relatively short , Then according to the Dictionary Compilation Rules "apple" > "app", because 'l' > '∅', among '∅' It's white space , Defined as smaller than any other character ( For more information ).
Tips :
1 <= words.length <= 100
1 <= words[i].length <= 20
order.length == 26
stay words[i] and order All the characters in are lowercase letters .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/verifying-an-alien-dictionary
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Their thinking :
* Their thinking : * hold order Convert to char Corresponding to order map, Then compare them in pairs to see if they conform to the dictionary order . * In the process of comparison , Judge from front to back , Every judge , * If index1>index2, It does not conform to the dictionary order ; * If index1<index2, In accordance with the dictionary order ; * If index1==index2 And not the last , Then judge the next ; * If index1==index2 And the last one , It does not conform to the dictionary order . such as apple,app.
Code :
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;
}
}边栏推荐
- 力扣解法汇总942-增减字符串匹配
- Force deduction solution summary 883- projected area of 3D shape
- Graphic data analysis | data cleaning and pretreatment
- Design principle [Demeter's Law]
- 力扣解法汇总面试题 17.11-单词距离
- Database
- How should programmers solve the problem of buying vegetables? Take you hand in hand to quickly order and grab vegetables by using the barrier free auxiliary function
- Ozzanmation action system based on SSE
- Swiftyjson analyse les fichiers json locaux
- "China Dongxin Cup" the fourth programming competition of Guangxi University (synchronous competition)
猜你喜欢

Linux(CentOS6)安装MySQL5.5版本数据库
![[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

Knowledge points of mall development

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

Google Ads 竞价的运作机制

Add sequence number column to MySQL query result set

SQL calculates KS, AUC, IV, psi and other risk control model indicators

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

What are the advantages of adaptive search advertising?

超图倾斜数据合并根节点后转3dtiles
随机推荐
DDD的分层架构
"China Dongxin Cup" the fourth programming competition of Guangxi University (synchronous competition)
Xcall cluster script (view JPS command)
Force deduction solution summary 449 serialization and deserialization binary search tree
力扣解法汇总-剑指 Offer II 114. 外星文字典
力扣解法汇总449-序列化和反序列化二叉搜索树
The most comprehensive redis transaction control in 2022 (with illustration)
State owned assets into shares, has Jianye real estate stabilized?
阿里云oss文件上传系统
el-upload上传文件
Alicloud OSS file upload system
Installing MySQL version 5.5 database for Linux (centos6)
PHP security development 13 column module of blog system
Force deduction solution summary 398 random number index
力扣解法汇总面试题 17.11-单词距离
Modification of system module information of PHP security development 12 blog system
程序员应该如何解决买菜难问题?手把手带你利用无障碍辅助功能快速下单抢菜
广泛匹配修饰符符号已经被弃用,请勿使用
力扣解法汇总433-最小基因变化
Installing mysql-5.7 for Linux (centos7)