原题链接在这里: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. CAS自旋volatile变量

    public final int getAndIncrement() { for (;;) { int current = get(); // 取得AtomicInteger里存储的数值 int ne ...

  2. 百度前端技术学院task13源代码

    突然发现只看书不练习也是不行的,这么简单的我竟然都不会写了. 要注意innerHTML,innerText和outText之间的异同. 同时也要会使用DOM2的添加事件,移除事件等 <!DOCT ...

  3. AGC039

    Contest Page A 对于一个长度为\(L\)的相同字符段,显然要花费\(\frac{L}{2}\)次操作才能使得相邻不相同.于是只需要分类讨论一下首尾字符是否相同,算出每种字符.每种长度的连 ...

  4. Web应用调用.Net Core API

    Web应用调用.Net Core API 一.新建Web Application应用: 选择Web Application 新建好之后页面如下: 二.新建Model.新建Model文件夹并建立apiM ...

  5. C#读写修改设置调整UVC摄像头画面-色调

    有时,我们需要在C#代码中对摄像头的色调进行读和写,并立即生效.如何实现呢? 建立基于SharpCamera的项目 首先,请根据之前的一篇博文 点击这里 中的说明,建立基于SharpCamera的摄像 ...

  6. C# 查看系统进程

    //使用前需要引用 using System.Diagnostics; var processList = Process.GetProcesses().ToList();

  7. python基础01day

    1 python多版本共存 因为python2和python3的解释器程序都是python.exe,在同时加入环境变量的情况下名称重复,如果重命名的话又会造成需要链接解释器的程序无法调用解释器,所以采 ...

  8. Julie D. Saba:儿童肿瘤学是一个令人惊奇的领域

    编者按 作为一名儿童肿瘤学家,工作中充满了挑战与机遇.近几十年来,世界各地的儿童肿瘤的发病率呈持续上升的趋势.许多人认为这不仅是由于诊断水平的提高,而且是因为儿童肿瘤的潜在风险也确实在增加.据英国儿童 ...

  9. spark和深度学习集成调研

    http://dy.163.com/v2/article/detail/E2TMAOTU0518KCLV.html http://www.elecfans.com/d/676451.html http ...

  10. 从 html 元素继承 box-sizing

    在大多数情况下我们在设置元素的 border 和 padding 并不希望改变元素的 width,height值,这个时候我们就可以为该元素设置 box-sizing:border-box;. 我不希 ...