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 <= 100
1 <= words[i].length <= 20
order.length == 26
- All characters in
words[i]
andorder
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的更多相关文章
- 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 ...
随机推荐
- ElasticSearch中的JVM性能调优
ElasticSearch中的JVM性能调优 前一段时间被人问了个问题:在使用ES的过程中有没有做过什么JVM调优措施? 在我搭建ES集群过程中,参照important-settings官方文档来的, ...
- 记:使用IScroll.js 开发picker日历组件遇到的问题及经验总结
IScroll中文文档 第一个问题: 边界留白 就是这种,上边界(最小),下边界(最大)有两个列表的位置是不能选择的.解决的办法是: 在HTML中,添加空白节点就行了. 第二个问题:初始化之后的滚动停 ...
- 运行带有Activiti modeler时,出现这样的报错: java.rmi.AccessException: Cannot modify this registry
最近在做整合Activiti Modeler工作流在线设计器的工作,运行IDEA时,出现了这样一个错误信息: 原因及解决办法: Activiti默认打开了jmx功能,默认端口为1099,idea中 ...
- .net HttpClient 回传实体帮助类
public class HttpClientHelper<T> { /// <summary> /// Get请求 返回实体 /// </summary> /// ...
- [08001] Could not create connection to database server. Attempted reconnect 3 times. Giving up IDEA2019的database插件无法链接mysql的解决办法(08001错误)
[08001] Could not create connection to database server. Attempted reconnect 3 times. Giving up. 点击这里 ...
- C# EventHandler观察者模式
C#和java比较: java中使用的是接口.C#使用委托机制,可以用时 + 运算符进行注册,直接多播. 而java中是一般是使用一个集合来保存观察者. 发布者(Publisher)= 被观察者 (O ...
- English--并列句
English|并列句 现在开始讲解英语中的最简单的长句,即并列句. 前言 目前所有的文章思想格式都是:知识+情感. 知识:对于所有的知识点的描述.力求不含任何的自我感情色彩. 情感:用我自己的方式, ...
- SQLi_Labs通关文档【1-65关】
SQLi_Labs通关文档[1-65关] 为了不干扰自己本机环境,SQL-LAB我就用的码头工人,跑起来的,搭建也非常简单,也就两条命令 docker pull acgpiano/sqli-labs ...
- 剑指前端(前端入门笔记系列)——DOM(元素节点)
DOM(元素节点) 本文介绍了元素节点的基本操作:增删改查 增 新增一个元素节点分为两步(二者缺一不可),第一步:创建元素节点,第二步:将创建的元素节点插入到指定元素节点中(也就是插入指定元素节点 ...
- Java 之 自定义异常
1.为什么需要自定义异常类 Java中不同的异常类,分别表示着某一种具体的异常情况,那么在开发中总是有些异常情况是没有定义好的,此时我们根据自己业务的异常情况来定义异常类. 一些异常都是 Java ...