原题链接在这里: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. HDU校赛 | 2019 Multi-University Training Contest 5

    2019 Multi-University Training Contest 5 http://acm.hdu.edu.cn/contests/contest_show.php?cid=852 100 ...

  2. NOI2019网络同步赛游记

    我发的邮件**f没收到,后来去专门询问才整到一个名额(估计是嫌我太菜,参加了也是垫底) day -1 上午写了到类似随机游走的高斯消元期望dp,然后颓颓颓 下午打洛咕月赛.T1一直50pts,后来才知 ...

  3. Java面试宝典(2020版)

    一.Java 基础 1. JDK 和 JRE 有什么区别? JDK:Java Development Kit 的简称,java 开发工具包,提供了 java 的开发环境和运行环境. JRE:Java ...

  4. C# - List.Sort()自定义排序方法

    本文通过示例介绍了C#中典型容器List.Sort()的自定义排序方法,进而引出了C#中自定义排序的核心接口及方法 项目地址:自定义Sort方法 - SouthBegonia's Github Lis ...

  5. 数据库‘master’中拒绝CREATE DATABASE权限

    今天在创建数据库的时候,遇到了没有创建数据库权限的问题,后来百度了一下解决了该问题. 1.先用windows身份验证登录,在安全性下面的找到自己创建的登录名,双击,在弹出的对话框中为它赋予权限. 2. ...

  6. dotnet core系列之Background tasks with hosted services (后台任务)

    这篇简单讲asp.net core 中的后台任务 用到的包: Microsoft.AspNetCore.App metapackage 或者加入 Microsoft.Extensions.Hostin ...

  7. 浏览器解析js

    网页加载js步骤 1.浏览器一边下载html网页,一边开始解析(不等下载完就解析)2.遇到<script>标签,暂停解析,网页渲染的控制权交给javascript引擎3.如果<scr ...

  8. Java自学-基本变量类型

    Java中的基本变量类型 一个变量的类型,决定了该变量可以包含什么样的值. Java中有八种基本类型,都是Java语言预先定义好的,并且是关键字. 这八种基本类型分别是: 整型 (4种) 字符型 (1 ...

  9. Springboot自动化部署到docker以及logback按天生成日志

    Dockerfile FROM java:8 VOLUME /tmp ADD maven/sms-0.0.1-SNAPSHOT.jar app.jar RUN sh -c 'touch /app.ja ...

  10. Unity手游汉化笔记①:UABE+AssetStudio编辑MonoBehavior类型Asset

    总的笔记:https://www.cnblogs.com/guobaoxu/p/12055930.html 目录 一.使用工具 二.具体操作 [1]利用AssetStudio进行预览 [2]UABE修 ...