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 ...
随机推荐
- SpringBoot 基础(二)
目录 SpringBoot基础(二) 一.操作数据库 1. SpringBootJdbc 2. SpringBoot 整合 Mybatis 3. SpringBott 使用JPA 二.使用 Thyme ...
- docker系列之三:docker实际应用
以Docker为基础完成持续集成.自动交付.自动部署: 原理: RD推送代码到git 仓库或者svn等代码服务器上面,git服务器就会通过hook通知jenkins. jenkine 克隆git代码到 ...
- 【leetcode-22】括号生成
给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结果为: [ "((()))", "(()())& ...
- 【java】单实例下的 流水号【21位】
单实例环境,不是分布式 需要流水号 /** * 流水号生成器 * * 年+天号+毫秒+随机数 * 2019+134+480+11位随机数 * 4+3+3+11 = 21位 * * * @author ...
- 关于vs无法创建虚拟目录的问题
插入链接:https://blog.csdn.net/zhao1955/article/details/92182935 补充:改完之后不要忘记以管理员的身份运行vs
- ubuntu开发常用收集
命令: 1.http://blog.csdn.net/simongeek/article/details/45271089 2.http://www.jianshu.com/p/654be9c0f13 ...
- WebAPI中路由参数中包含字符-点“.”
请求url都是类似:/api/area/province.list 我们默认建立的Asp Net WebApi 服务时,如果请求url包含“.”,则返回404错误. 解决办法:需要在web.confi ...
- 接口XMPPConnection
接口XMPPConnection 所有已知的实现类: AbstractXMPPConnection,XMPPBOSHConnection,XMPPTCPConnection 公共接口XMPPConne ...
- C#控制IIS动态添加删除网站
我的目的是在Winform程序里面,可以直接启动一个HTTP服务端,给下游客户连接使用. 查找相关技术,有两种方法: 1.使用C#动态添加网站应用到IIS中,借用IIS的管理能力来提供HTTP接口.本 ...
- MySQL--用户管理 pymysql 索引
目录 用户管理 创建mysql账户 权限管理 涉及到的表 pymysql 索引 语法 结论 用户管理 主要是为了控制权限,让不同的人只能操作只属于只记得那部分数据 创建mysql账户 账户中涉及三个数 ...