当前位置: 首页 > java >正文

串联所有单词的子串-leetcode

  • 给定一个字符串 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]
解释:因为 words.length == 2 同时 words[i].length == 3,连接的子字符串的长度必须为 6。
子串 “barfoo” 开始位置是 0。它是 words 中以 [“bar”,“foo”] 顺序排列的连接。
子串 “foobar” 开始位置是 9。它是 words 中以 [“foo”,“bar”] 顺序排列的连接。
输出顺序无关紧要。返回 [9,0] 也是可以的。
示例 2:

输入:s = “wordgoodgoodgoodbestword”, words = [“word”,“good”,“best”,“word”]
输出:[]
解释:因为 words.length == 4 并且 words[i].length == 4,所以串联子串的长度必须为 16。
s 中没有子串长度为 16 并且等于 words 的任何顺序排列的连接。
所以我们返回一个空数组。
示例 3:

输入:s = “barfoofoobarthefoobarman”, words = [“bar”,“foo”,“the”]
输出:[6,9,12]
解释:因为 words.length == 3 并且 words[i].length == 3,所以串联子串的长度必须为 9。
子串 “foobarthe” 开始位置是 6。它是 words 中以 [“foo”,“bar”,“the”] 顺序排列的连接。
子串 “barthefoo” 开始位置是 9。它是 words 中以 [“bar”,“the”,“foo”] 顺序排列的连接。
子串 “thefoobar” 开始位置是 12。它是 words 中以 [“the”,“foo”,“bar”] 顺序排列的连接。

class Solution
{
public:vector<int> findSubstring(string s, vector<string> &words){if (s.empty() || words.empty()){return {};}// 单词长度int one_word_len = words[0].size();// 单词数量int words_count = words.size();// 所需长度int required_len = one_word_len * words_count;if (s.size() < required_len){return {};}// required_map为每个单词所需要出现的次数unordered_map<string, int> required_map;for (const auto &str : words){required_map[str]++;}vector<int> res;// 动态滑动窗口起点for (int i = 0; i < one_word_len; i++){int left = i;int right = i;int count = 0;unordered_map<string, int> seen_map;// 外层循环控制窗口右边界,每次增长大小为一个单词的长度for (; right < (s.size() - one_word_len + 1); right += one_word_len){string right_str = s.substr(right, one_word_len);seen_map[right_str]++;count++;if (required_map.find(right_str) != required_map.end()){// 当前单词数虽然是所需单词,但是已经超过了我们所需次数,需要缩减左窗口,直到单词数符合要求while (seen_map[right_str] > required_map[right_str]){string left_str = s.substr(left, one_word_len);seen_map[left_str]--;count--;left += one_word_len;}}else{ // 当前单词不是我们所需单词,左指针直接跳到这里,重新开始往后滑seen_map.clear();count = 0;left = right + one_word_len;}// 如果当前窗口内的单词数=words内的单词数,则找到if (count == words_count){res.push_back(left);}}}return res;}
};
http://www.xdnf.cn/news/17269.html

相关文章:

  • 解读 gpt-oss-120b 和 gpt-oss-20b开源模型
  • 多账号管理方案:解析一款免Root的App分身工具
  • 抖音、快手、视频号等多平台视频解析下载 + 磁力嗅探下载、视频加工(提取音频 / 压缩等)
  • 编程之线性代数矩阵和概率论统计知识回顾
  • 基于langchain的两个实际应用:[MCP多服务器聊天系统]和[解析PDF文档的RAG问答]
  • 表单元素与美化技巧:打造用户友好的交互体验
  • 基于Ruby的IP池系统构建分布式爬虫架构
  • Qt帮助文档跳转问题修复指南
  • Flink-1.19.0源码详解9-ExecutionGraph生成-后篇
  • 通信中间件 Fast DDS(一) :编译、安装和测试
  • 汽车线束设计—导线的选取
  • WEB开发-第二十七天(PHP篇)
  • 中国MCP市场:腾讯、阿里、百度的本土化实践
  • Disruptor 消费者核心:BatchEventProcessor解析
  • 脱机部署k3s
  • 嵌入式硬件中MOSFET基本控制详解
  • 前端开发(HTML,CSS,VUE,JS)从入门到精通!第七天(Vue)(二)
  • FluentUI的介绍与使用案列
  • Pytest项目_day06(requests中Session的用法)
  • Spring文件泄露与修复方案总结
  • Go语言版JSON转TypeScript接口生成器:支持智能递归解析与命名优化
  • [Python 基础课程]Set
  • [Oracle] ROUND()函数
  • ORACLE 19C建库时卡在46%、36%
  • 《设计模式之禅》笔记摘录 - 13.迭代器模式
  • Kaggle 经典竞赛泰坦尼克号:超级无敌爆炸详细基础逐行讲解Pytorch实现代码,看完保证你也会!!!
  • 数据结构 二叉树(1)二叉树简单了解
  • 指挥中心自动化的演变
  • 状态模式及优化
  • 3479. 水果成篮 III