原题链接在这里: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 <= 100
  • 1 <= words[i].length <= 20
  • order.length == 26
  • All characters in words[i] and order are 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的更多相关文章

  1. LeetCode 953 Verifying an Alien Dictionary 解题报告

    题目要求 In an alien language, surprisingly they also use english lowercase letters, but possibly in a d ...

  2. LeetCode 953. Verifying an Alien Dictionary (验证外星语词典)

    题目标签:HashMap 题目给了我们一个 order 和 words array,让我们依照order 来判断 words array 是否排序. 利用hashmap 把order 存入 map, ...

  3. 【Leetcode_easy】953. Verifying an Alien Dictionary

    problem 953. Verifying an Alien Dictionary solution: class Solution { public: bool isAlienSorted(vec ...

  4. 【leetcode】953. Verifying an Alien Dictionary

    题目如下: In an alien language, surprisingly they also use english lowercase letters, but possibly in a ...

  5. 【LeetCode】953. Verifying an Alien Dictionary 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. 953.Verifying an Alien Dictionary(Map)

    In an alien language, surprisingly they also use english lowercase letters, but possibly in a differ ...

  7. Verifying an Alien Dictionary

    2019-11-24 22:11:30 953. Verifying an Alien Dictionary 问题描述: 问题求解: 这种问题有一种解法是建立新的排序和abc排序的映射,将这里的str ...

  8. LeetCode.953-验证外语字典顺序(Verifying an Alien Dictionary)

    这是悦乐书的第364次更新,第392篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第226题(顺位题号是953).在外语中,令人惊讶的是,他们也使用英文小写字母,但可能使 ...

  9. [Swift]LeetCode953. 验证外星语词典 | Verifying an Alien Dictionary

    In an alien language, surprisingly they also use english lowercase letters, but possibly in a differ ...

随机推荐

  1. C++ 01 基础知识点

    1.为某一特定的问题而设计的指令序列称为:程序 2.‘32位微型计算机’中的32位指的是:机器的字长 3.存储设备中,存取速度最快的是:Cache 4.指令的操作码表示的是:作何操作 5.一条指令由哪 ...

  2. Python格式化输出——format用法示例

    format OR % 提到Python中的格式化输出方法,一般来说有以下两种方式: print('hello %s' % 'world') # hello world print('hello {} ...

  3. Java的常用API之System类简介

    Syetem类 java.lang.System类中提供了大量的静态方法,可以获取与系统相关的信息或系统级操作,在System类的API文档中,常用的方法有: public static long c ...

  4. JWT与RBAC权限模型

    JWT JWT是什么? Json web token (JWT)是为了网络应用环境间传递声明而执行的一种基于JSON的开发标准(RFC7519),该token被设计为紧凑且安全的,特别适用于分布式站点 ...

  5. (10)ASP.NET Core 中的环境(Environments:dev, stage, prod)

    1.环境变量配置 ASP.NET Core在应用程序启动时读取环境变量(Properties\launchSettings.json)ASPNETCORE_ENVIRONMENT,并将该值存储在IHo ...

  6. 立个铁矿石的flag,从7月初开始,铁矿石的库存,可能要进入累库存阶段了.

    从发货量倒推出的到货量,用来评估未来的到货量 推测的到港量与实际北方6港到港量的关系 通过月度到港量,估计出北方6港对全国到港量的正确性. 悲观的库存预期 乐观的库存预期 大概率的情况吧

  7. 史上最全HashMap红黑树解析

    HashMap红黑树解析 红黑树介绍 TreeNode结构 树化的过程 红黑树的左旋和右旋 TreeNode的左旋和右旋 红黑树的插入 TreeNode的插入 红黑树的删除 TreeNode的删除节点 ...

  8. GitLab CI/CD的官译【原】

    CI / CD方法简介 软件开发的持续集成基于自动执行脚本,以最大限度地减少在开发应用程序时引入错误的可能性.从新代码的开发到部署,它们需要较少的人为干预甚至根本不需要干预. 它涉及在每次小迭代中不断 ...

  9. 解决Ubuntu18.10 网络图标经常消失连不上网问题

    我不知道是什么原因,Ubuntu虚拟机经常会出现无法上网的问题? 此时右上角没有网络标志,Settings->NetWork也只有VPN一项,不知道咋用. 在网上终于找到了方法,亲测有效:htt ...

  10. 字符串转换成float和double类型

    double strtod(const char *nptr, char **endptr); float strtof(const char *nptr, char **endptr); long ...