LeetCode 953. Verifying an Alien Dictionary
原题链接在这里:https://leetcode.com/problems/verifying-an-alien-dictionary/
题目:
In an alien language, surprisingly they also use english lowercase letters, but possibly in a different order. The order of the alphabet is some permutation of lowercase letters.
Given a sequence of words written in the alien language, and the order of the alphabet, return true if and only if the given words are sorted lexicographicaly in this alien language.
Example 1:
Input: words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
Output: true
Explanation: As 'h' comes before 'l' in this language, then the sequence is sorted.
Example 2:
Input: words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
Output: false
Explanation: As 'd' comes after 'l' in this language, then words[0] > words[1], hence the sequence is unsorted.
Example 3:
Input: words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz"
Output: false
Explanation: The first three characters "app" match, and the second string is shorter (in size.) According to lexicographical rules "apple" > "app", because 'l' > '∅', where '∅' is defined as the blank character which is less than any other character (More info).
Constraints:
1 <= words.length <= 1001 <= words[i].length <= 20order.length == 26- All characters in
words[i]andorderare English lowercase letters.
题解:
Given the order in string order, covert it into char with its index relationship.
For current and previous string, check if previous string is bigger than current string. If yes, return false.
In order to check if previous string is bigger than current string, get the corresponding char, when they are not equal, check if previous index is bigger than current one, if yes, return false.
If they keep equal until the end, then check if previous string length > current string length.
Time Complexity: O(nm + k). n = words.length(). m is average length. k = order.length(). k <= 26.
Space: O(1).
AC Java:
class Solution {
public boolean isAlienSorted(String[] words, String order) {
if(words == null || words.length == 0 || order == null || order.length() == 0){
return true;
}
int [] map = new int[26];
for(int i = 0; i<order.length(); i++){
map[order.charAt(i)-'a'] = i;
}
for(int i = 1; i<words.length; i++){
if(isBigger(words[i-1], words[i], map)){
return false;
}
}
return true;
}
private boolean isBigger(String s1, String s2, int [] map){
int j = 0;
while(j < s1.length() && j < s2.length()){
if(s1.charAt(j) != s2.charAt(j)){
return map[s1.charAt(j) - 'a'] > map[s2.charAt(j) - 'a'];
}
j++;
}
return s1.length() > s2.length();
}
}
LeetCode 953. Verifying an Alien Dictionary的更多相关文章
- LeetCode 953 Verifying an Alien Dictionary 解题报告
题目要求 In an alien language, surprisingly they also use english lowercase letters, but possibly in a d ...
- LeetCode 953. Verifying an Alien Dictionary (验证外星语词典)
题目标签:HashMap 题目给了我们一个 order 和 words array,让我们依照order 来判断 words array 是否排序. 利用hashmap 把order 存入 map, ...
- 【Leetcode_easy】953. Verifying an Alien Dictionary
problem 953. Verifying an Alien Dictionary solution: class Solution { public: bool isAlienSorted(vec ...
- 【leetcode】953. Verifying an Alien Dictionary
题目如下: In an alien language, surprisingly they also use english lowercase letters, but possibly in a ...
- 【LeetCode】953. Verifying an Alien Dictionary 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 953.Verifying an Alien Dictionary(Map)
In an alien language, surprisingly they also use english lowercase letters, but possibly in a differ ...
- Verifying an Alien Dictionary
2019-11-24 22:11:30 953. Verifying an Alien Dictionary 问题描述: 问题求解: 这种问题有一种解法是建立新的排序和abc排序的映射,将这里的str ...
- LeetCode.953-验证外语字典顺序(Verifying an Alien Dictionary)
这是悦乐书的第364次更新,第392篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第226题(顺位题号是953).在外语中,令人惊讶的是,他们也使用英文小写字母,但可能使 ...
- [Swift]LeetCode953. 验证外星语词典 | Verifying an Alien Dictionary
In an alien language, surprisingly they also use english lowercase letters, but possibly in a differ ...
随机推荐
- GIT 安装和使用
目录 GIT 安装和使用 一.GIT 介绍 二.GIT 安装 三.GIT 使用 1. 配置 2. 创建版本库 3. 远程仓库 4. 分支管理 5.标签管理 6. 自定义 GIT 安装和使用 一.GIT ...
- golang开始篇
一 First Golang 1.1 需求 第一个程序hello.go,可以输出"hello golang" 1.2 开发步骤 开发这个程序时,我们的目录结构怎么处理(让自己或 ...
- [Atcoder AGC029C]Lexicographic constraints
题目大意:给定$n$个字符串的长度$a_i$,问至少用几种字符可以构造出字符串$s_1\sim s_n$,满足$|s_i|=a_i$且$s_1<s_2<\cdots<s_n$. $ ...
- intellij idea 修改背景保护色&&修改字体&&快捷键大全
intellij idea 修改背景保护色&&修改字体&&快捷键大全 原创 2013年11月22日 18:00:07 90176 最近Idea比较流行,Eclipse因 ...
- c#栈的用法
栈是一种重要的线性结构,栈和队列是限定插入和删除只能在表的“端点”进行的线性表 –栈的元素必须“后进先出”. –栈的操作只能在这个线性表的表尾进行. –注:对于栈来说,这个表尾称为栈的栈顶(top), ...
- c# 读取文件目录下的信息
private void button1_Click(object sender, System.EventArgs e) { //浏览文件夹 this.folderBrowserDialog1.Sh ...
- 走一次HashMap的存取
忘了太多东西,好好复习. 存: if ((tab = table) == null || (n = tab.length) == 0) n = (tab = resize()).length;//检查 ...
- Android studio module生成jar包,module中引用的第三方库没有被引用,导致java.lang.NoClassDefFoundError错误。
android studio 创建了一个Module生成jar包,这个module中有引用一些第三方的类库,比如 gson,volley等. 但是生成的jar包里,并没有将gson,volley等第三 ...
- Android编译系统中的Android.bp
https://www.cnblogs.com/bluestorm/p/10895005.html Android.bp,是用来替换Android.mk的配置文件. 它使用Blueprint框架来解析 ...
- SAP CDS重定向视图和直接读这两者场景的性能比较
A very rough performance comparison is performed in ER9/001. Comparison scenario The two below opera ...