[LeetCode] 953. 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 lexicographically 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.
验证外星语字典。
某种外星语也使用英文小写字母,但可能顺序 order 不同。字母表的顺序(order)是一些小写字母的排列。
给定一组用外星语书写的单词 words,以及其字母表的顺序 order,只有当给定的单词在这种外星语中按字典序排列时,返回 true;否则,返回 false。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/verifying-an-alien-dictionary
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题目给了一些单词和一个定好的字典序,请你验证单词是否是有序的。这道题看上去跟经典题 269 很像但是简单很多。思路是把 order 里面的字母之间的顺序转化为 hashmap<letter, order>,然后再对 words 里面的单词进行两两比较。比较的方式是按字母逐个比较,如果同样位置上的两个字母不一样,则判断是否第一个单词的字母的字典序更小,若不是就 return false;如果判断完了并没有找到 false 的情形,则判断是否第一个单词更短,因为除了满足字母之间的顺序之外,字典序也需要满足更短的单词在前。
时间O(n)
空间O(n)
Java实现
1 class Solution { 2 public boolean isAlienSorted(String[] words, String order) { 3 HashMap<Character, Integer> map = new HashMap<>(); 4 for (int i = 0; i < order.length(); i++) { 5 char ch = order.charAt(i); 6 map.put(ch, i); 7 } 8 9 for (int i = 0; i < words.length - 1; i++) { 10 if (!inorder(words[i], words[i + 1], map)) { 11 return false; 12 } 13 } 14 return true; 15 } 16 17 private boolean inorder(String w1, String w2, HashMap<Character, Integer> map) { 18 for (int i = 0; i < w1.length() && i < w2.length(); i++) { 19 char ch1 = w1.charAt(i); 20 char ch2 = w2.charAt(i); 21 int index1 = map.get(ch1); 22 int index2 = map.get(ch2); 23 if (index1 < index2) { 24 return true; 25 } else if (index1 > index2) { 26 return false; 27 } 28 } 29 return w1.length() <= w2.length(); 30 } 31 }
相关题目
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· PPT革命!DeepSeek+Kimi=N小时工作5分钟完成?
· What?废柴, 还在本地部署DeepSeek吗?Are you kidding?
· DeepSeek企业级部署实战指南:从服务器选型到Dify私有化落地
· 程序员转型AI:行业分析
· 重磅发布!DeepSeek 微调秘籍揭秘,一键解锁升级版全家桶,AI 玩家必备神器!