当前位置:网站首页>leetcode:269. 火星词典
leetcode:269. 火星词典
2022-08-05 00:10:00 【OceanStar的学习笔记】
题目来源
题目描述
现有一种使用英语字母的火星语言,这门语言的字母顺序与英语顺序不同。
给你一个字符串列表 words ,作为这门语言的词典,words 中的字符串已经 按这门新语言的字母顺序进行了排序 。
请你根据该词典还原出此语言中已知的字母顺序,并 按字母递增顺序 排列。若不存在合法字母顺序,返回 “” 。若存在多种可能的合法字母顺序,返回其中 任意一种 顺序即可。
字符串 s 字典顺序小于 字符串 t 有两种情况:
- 在第一个不同字母处,如果 s 中的字母在这门外星语言的字母顺序中位于 t 中字母之前,那么 s 的字典顺序小于 t 。
- 如果前面 min(s.length, t.length) 字母都相同,那么 s.length < t.length 时,s 的字典顺序也小于 t 。
示例 1:
输入:words = [“wrt”,“wrf”,“er”,“ett”,“rftt”]
输出:“wertf”
示例 2:
输入:words = [“z”,“x”]
输出:“zx”
示例 3:
输入:words = [“z”,“x”,“z”]
输出:“”
解释:不存在合法字母顺序,因此返回 “” 。
提示:
1 <= words.length <= 100
1 <= words[i].length <= 100
words[i] 仅由小写英文字母组成
思路
思路
- 可以先根据word生成一张图
- 比较相邻两个words,逐个比较两个words相同位置的字符,直到找到第一个字符不相同的位置
- 这个位置的两个字符我们可以获得有效信息,也就是这两个字符在alien order中的相对顺序
- 需要注意的是也就是这个位置有用,因为后面的不同位置我们无法获取任何信息。
- 然后看拓扑排序,不能有环
举个例子
比如题目中给的例子1,可以推出的顺序关系有
t->f
w->e
r->t
e->r
实现
class Solution {
public:
std::string alienOrder(vector<std::string>& words) {
if(words.empty()){
return "";
}
int N = words.size();
// 将出现过的字符的入度设定为0
// 没有出现的字母入度为-1,出现了的字母的入度为非负数
std::map<char, int> indegree;
for (int i = 0; i < N; ++i) {
for(char ch : words[i]){
indegree[ch] = 0;
}
}
std::map<char, std::set<char>> graph;
for (int i = 0; i < N - 1; ++i) {
std::string curr = words[i];
std::string next = words[i + 1];
int len = std::min(curr.length(), next.length());
int j = 0;
for(;j < len; j++){
// # 保存第一个不同的字符顺序,位于前面的字母作为key,位于后面的字母存到set中作为值
if(curr[j] != next[j]){
if(!graph[curr[j]].count(next[j])){
graph[curr[j]].emplace(next[j]);
indegree[next[j]]++;
}
break;
}
}
// a 的字母排列顺序优先于 b,那么在给定的词典当中 a 定先出现在 b 前面
if(j < curr.length() && j == next.length()){
return ""; //前面都一样,cur字符更长,next没有字符了----说明给出的字典就不对
}
}
std::queue<char> q;
for(auto it : indegree){
char key = it.first;
if(indegree[key] == 0){
q.push(key);
}
}
std::string ans;
while (!q.empty()){
char curr = q.front(); q.pop();
ans += curr;
if(graph.count(curr)){
for(char next : graph[curr]){
--indegree[next];
if(indegree[next] == 0){
q.push(next);
}
}
}
}
return ans.length() == indegree.size() ? ans : "";
}
};
测试
int main() {
std::vector<std::string> vec {
"wrt",
"wrf",
"er",
"ett",
"rftt"};
Solution a;
auto v = a.alienOrder(vec) ;
printf("%s\n", v.c_str());
return 1;
}
参考
边栏推荐
- 【云原生--Kubernetes】Pod控制器
- #yyds干货盘点#交换设备丢包严重的故障处理
- 【LeetCode】滑动窗口题解汇总
- 建模师经验分享:模型学习方法
- How to automatically push my new articles to my fans (very simple, can't learn to hit me)
- 对写作的一些感悟
- Bidding Announcement | Operation and Maintenance Project of Haina Baichuang Official Account
- 游戏3D建模入门,有哪些建模软件可以选择?
- Xiaohei leetcode surfing: 94. Inorder traversal of binary tree
- Security software Avast and Symantec NortonLifeLock merge with UK approval, market value soars 43%
猜你喜欢

Implementation principle of golang coroutine

《MySQL入门很轻松》第2章:MySQL管理工具介绍

Getting started with 3D modeling for games, what modeling software can I choose?

Basic web in PLSQL

【云原生--Kubernetes】Pod控制器

2 用D435i运行VINS-fusion

【LeetCode】Summary of Two Pointer Problems
![情侣牵手[贪心 & 抽象]](/img/7d/1cafc000dc58f1c5e2e92150be7953.png)
情侣牵手[贪心 & 抽象]
![[Happy Qixi Festival] How does Nacos realize the service registration function?](/img/df/5793145da45bc80d227b0babfac914.png)
[Happy Qixi Festival] How does Nacos realize the service registration function?

如何写好测试用例
随机推荐
软件质量评估的通用模型
从单体架构迁移到 CQRS 后,我觉得 DDD 并不可怕
[LeetCode] Summary of Matrix Simulation Related Topics
再肝3天,整理了90个 NumPy 例子,不能不收藏!
Xiaohei's leetcode journey: 95. Longest substring with at least K repeating characters
Huggingface入门篇 II (QA)
The applicable scenarios and common product types of the KT148A electronic voice chip ic solution
Cython
jenkins send mail system configuration
网站最终产品页使用单一入口还是多入口?
没有这些「伪需求」,产品经理的 KPI 怎么完成?
元宇宙:未来我们的每一个日常行为是否都能成为赚钱工具?
招标公告 | 海纳百创公众号运维项目
上课笔记(6)(2)——#742. 周末舞会
Chinese and Japanese color style
uniapp 分享功能-分享给朋友群聊朋友圈效果(整理)
性能测试如何准备测试数据
STC89C52RC的P4口的应用问题
Uniapp dynamic sliding navigation effect demo (finishing)
VMware NSX 4.0 -- 网络安全虚拟化平台