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

串联所有单词的子串-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.dtcms.com/a/319701.html

相关文章:

  • 计算机基础·linux系统
  • Linux线程学习
  • pytorch学习笔记-最大池化maxpooling的使用、搭建多层网络并验证、sequential的使用
  • golang的面向对象编程,struct的使用
  • 2.8 逻辑符号
  • Linux怎么查看时区信息?(Linux时区)(tzselect)
  • Java中接口与抽象类
  • 处理失败: module ‘fitz‘ has no attribute ‘open‘
  • 传统防火墙与下一代防火墙
  • 华为 2025 校招目标院校
  • 【2025最新】在 macOS 上构建 Flutter iOS 应用
  • 嵌入式学习---在 Linux 下的 C 语言学习 Day10
  • 可执行文件的生成与加载执行
  • 超高车辆如何影响城市立交隧道安全?预警系统如何应对?
  • [论文阅读] 软件工程 | 软件工程中的同理心:表现、动机与影响因素解析
  • oracle 11G安装大概率遇到问题
  • 大文件断点续传(vue+springboot+mysql)
  • Failed to restart docker.service: Unit docker.service is masked.
  • PostgreSQL 数据库 设置90天密码过期时间的完整方案
  • 读取了错误数据导致STM32 单片机Hard Fault
  • 智能升级革命:Deepoc具身模型开发板如何让传统除草机器人拥有“认知大脑”
  • 分布式微服务--GateWay(过滤器及使用Gateway注意点)
  • 翻译模型(TM):基于短语的统计翻译模型(PBSMT)的构建
  • C++语法与面向对象特性(2)
  • PyTorch如何实现婴儿哭声检测和识别
  • 目标检测数据集 - 自动驾驶场景道路异常检测数据集下载「包含VOC、COCO、YOLO三种格式」
  • 接口自动化-pytest
  • OpenAI 开源模型 gpt-oss 正式上线微软 Foundry 平台
  • 网络可视,运维无忧:分钟级定位,告别盲目扩容
  • 从零开始构建情绪可视化日记平台 - React + TypeScript + Vite