目录题目描述思路分析AC 代码题目描述 953. 验证外星语词典 某种外星语也使用英文小写字母,但可能顺序 order 不同。字母表的顺序(order)是一些小写字母的排列。 给
953. 验证外星语词典
某种外星语也使用英文小写字母,但可能顺序 order
不同。字母表的顺序(order
)是一些小写字母的排列。
给定一组用外星语书写的单词 Words
,以及其字母表的顺序 order
,只有当给定的单词在这种外星语中按字典序排列时,返回 true
;否则,返回 false
。
示例 1:
输入:words = ["hello","LeetCode"], order = "hlabcdefgijkmnopqrstuvwxyz"
输出:true
解释:在该语言的字母表中,'h' 位于 'l' 之前,所以单词序列是按字典序排列的。
示例 2:
输入:words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
输出:false
解释:在该语言的字母表中,'d' 位于 'l' 之后,那么 words[0] > words[1],因此单词序列不是按字典序排列的。
示例 3:
输入:words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz"
输出:false
解释:当前三个字符 "app" 匹配时,第二个字符串相对短一些,然后根据词典编纂规则 "apple" > "app",因为 'l' > '∅',其中 '∅' 是空白字符,定义为比任何其他字符都小(更多信息)。
提示:
1 <= words.length <= 100
1 <= words[i].length <= 20
order.length == 26
在 words[i] 和 order 中的所有字符都是英文小写字母。
class Solution {
public:
bool compare(string& a, string& b, unordered_map<char, int>& mp) {
int sizeA = a.size();
int sizeB = b.size();
int minLen = sizeA > sizeB ? sizeB : sizeA;
int same = 0;
for (int i = 0; i < minLen; i++) {
if (mp[a[i]] < mp[b[i]]) {
return true;
} else if(mp[a[i]] > mp[b[i]]) {
return false;
}else if (mp[a[i]] == mp[b[i]]) {
same++;
}
}
if (same == minLen) {
return a.size() < b.size();
}
return false;
}
bool isAlienSorted(vector<string>& words, string order) {
unordered_map<char, int> mp;
for (int i = 0; i < 26; i++) {
mp[order[i]] = i;
}
int size = words.size();
if (size == 1) {
return true;
}
for (int i = 0; i < (size - 1); i++) {
if (compare(words[i], words[i+1], mp) == false) {
return false;
}
}
return true;
}
};
以上就是Go语言leetcode题解953验证外星语词典示例详解的详细内容,更多关于Go 验证外星语词典的资料请关注编程网其它相关文章!
--结束END--
本文标题: Go语言leetcode题解953验证外星语词典示例详解
本文链接: https://www.lsjlt.com/news/176061.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-04-05
2024-04-05
2024-04-05
2024-04-05
2024-04-05
2024-04-05
2024-04-05
2024-04-05
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0