LeetCode 第30题:串联所有单词的子串
1.LeetCode 第10题:正则表达式匹配2.LeetCode 第1题:两数之和3.LeetCode 第2题:两数相加4.LeetCode 第3题:无重复字符的最长子串5.LeetCode 第4题:寻找两个正序数组的中位数6.LeetCode 第8题:字符串转换整数 (atoi)7.LeetCode 第7题:整数反转8.LeetCode 第6题:Z字形变换9.LeetCode 第5题:最长回文子串10.LeetCode 第9题:回文数11.LeetCode 第11题:盛最多水的容器12.LeetCode 第12题:整数转罗马数字13.LeetCode 第13题:罗马数字转整数14.LeetCode 第14题:最长公共前缀15.LeetCode 第15题:三数之和16.LeetCode 第16题:最接近的三数之和17.LeetCode 第17题:电话号码的字母组合18.LeetCode 第18题:四数之和19.LeetCode 第19题:删除链表的倒数第N个结点20.LeetCode 第20题:有效的括号21.LeetCode 第21题:合并两个有序链表22.LeetCode 第22题:括号生成23.LeetCode 第23题:合并K个升序链表24.LeetCode 第24题:两两交换链表中的节点25.LeetCode 第25题:K 个一组翻转链表26.LeetCode 第26题:删除有序数组中的重复项27.LeetCode 第27题:移除元素28.LeetCode 第28题:找出字符串中第一个匹配项的下标29.LeetCode 第29题:两数相除
30.LeetCode 第30题:串联所有单词的子串
31.LeetCode 第31题:下一个排列32.LeetCode 第32题:最长有效括号33.LeetCode 第33题:搜索旋转排序数组34.LeetCode 第34题:在排序数组中查找元素的第一个和最后一个位置35.LeetCode 第35题:搜索插入位置36.LeetCode 第36题:有效的数独37.LeetCode 第37题:解数独38.LeetCode 第38题:外观数列39.LeetCode 第39题:组合总和40.LeetCode 第40题:组合总和 II41.LeetCode 第41题:缺失的第一个正数LeetCode 第30题:串联所有单词的子串
题目描述
给定一个字符串 s 和一个字符串数组 words。 words 中所有字符串 长度相同。
s 中的 串联子串 是指一个包含 words 中所有字符串以任意顺序排列连接起来的子串。
例如,如果 words = ["ab","cd","ef"], 那么 "abcdef", "abefcd","cdabef", "cdefab","efabcd", 和 "efcdab" 都是串联子串。 "acdbef" 不是串联子串,因为他不是任何 words 排列的连接。
返回所有串联子串在 s 中的开始索引。你可以以 任意顺序 返回答案。
难度
困难
题目链接
示例
示例 1:
输入:s = "barfoothefoobarman", words = ["foo","bar"]
输出:[0,9]
解释:串联子串的起始位置为:
- 0:"barfoo" 是 ["bar","foo"] 的串联
- 9:"foobar" 是 ["foo","bar"] 的串联
示例 2:
输入:s = "wordgoodgoodgoodbestword", words = ["word","good","best","word"]
输出:[]
解释:不存在串联子串
提示
1 <= s.length <= 104
1 <= words.length <= 5000
1 <= words[i].length <= 30
words[i]
和s
由小写英文字母组成
解题思路
方法:滑动窗口 + 哈希表
这是一道综合性的字符串处理题目,需要结合滑动窗口和哈希表来解决。
关键点:
- 所有单词长度相同,这是一个重要的条件
- 需要考虑所有可能的起始位置
- 使用哈希表记录单词出现次数
具体步骤:
- 预处理:统计words中每个单词的出现次数
- 滑动窗口:遍历所有可能的起始位置
- 验证过程:检查当前窗口中的单词是否符合要求
图解思路
滑动窗口过程表
步骤 | 窗口内容 | 单词分割 | 状态 | 说明 |
---|---|---|---|---|
初始状态 | "barfoo" | ["bar","foo"] | 有效 | 找到第一个有效位置 0 |
移动窗口 | "arfoot" | ["arf","oot"] | 无效 | 不是有效的单词组合 |
继续移动 | "foobar" | ["foo","bar"] | 有效 | 找到第二个有效位置 9 |
哈希表状态分析
阶段 | 目标哈希表 | 当前哈希表 | 匹配状态 |
---|---|---|---|
初始化 | {} | 未开始 | |
第一次匹配 | 完全匹配 | ||
中间状态 | 不匹配 | ||
第二次匹配 | 完全匹配 |
代码实现
public class Solution {
public IList<int> FindSubstring(string s, string[] words) {
List<int> result = new List<int>();
if (string.IsNullOrEmpty(s) || words == null || words.Length == 0) {
return result;
}
Dictionary<string, int> wordCount = new Dictionary<string, int>();
foreach (string word in words) {
if (!wordCount.ContainsKey(word)) {
wordCount[word] = 0;
}
wordCount[word]++;
}
int wordLength = words[0].Length;
int totalLength = wordLength * words.Length;
for (int i = 0; i <= s.Length - totalLength; i++) {
Dictionary<string, int> seenWords = new Dictionary<string, int>();
int j;
for (j = 0; j < words.Length; j++) {
int startPos = i + j * wordLength;
string currentWord = s.Substring(startPos, wordLength);
if (!wordCount.ContainsKey(currentWord)) {
break;
}
if (!seenWords.ContainsKey(currentWord)) {
seenWords[currentWord] = 0;
}
seenWords[currentWord]++;
if (seenWords[currentWord] > wordCount[currentWord]) {
break;
}
}
if (j == words.Length) {
result.Add(i);
}
}
return result;
}
}
执行结果
- 执行用时:276 ms
- 内存消耗:45.8 MB
代码亮点
- 🎯 使用哈希表高效统计单词频率
- 💡 滑动窗口优化时间复杂度
- 🔍 精确的单词匹配逻辑
- 🎨 清晰的代码结构和变量命名
常见错误分析
- 🚫 没有处理空字符串或空数组的情况
- 🚫 忘记检查单词长度是否相同
- 🚫 哈希表比较逻辑错误
- 🚫 子串长度计算错误
解法对比
解法 | 时间复杂度 | 空间复杂度 | 优点 | 缺点 |
---|---|---|---|---|
暴力匹配 | O(n * m * k) | O(m * k) | 实现简单 | 效率低 |
滑动窗口 | O(n * k) | O(m) | 效率较高 | 实现复杂 |
优化滑动窗口 | O(n) | O(m) | 效率最高 | 需要额外空间 |
相关题目
- LeetCode 第3题:无重复字符的最长子串 - 中等
- LeetCode 第76题:最小覆盖子串 - 困难
- LeetCode 第438题:找到字符串中所有字母异位词 - 中等
- LeetCode 第567题:字符串的排列 - 中等
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了