LeetCode 30. Substring with Concatenation of All Words

通达信+大智慧数据同步存调DLL 数据同步存调DLL(通达信版+大智慧版) 阅读详情

30. Substring with Concatenation of All Words

You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.

Example 1:

Input:
s = “barfoothefoobarman”,
words = [“foo”,“bar”]
Output: [0,9]
Explanation: Substrings starting at index 0 and 9 are “barfoor” and “foobar” respectively.
The output order does not matter, returning [9,0] is fine too.
Example 2:

Input:
s = “wordgoodgoodgoodbestword”,
words = [“word”,“good”,“best”,“word”]
Output: []

Approach

题目大意:从一组字符串中找到连续一串与words所有字符串组合相同
解题思路:使用滑动窗口的思想,设words中单词长为len,我们就可以从字符串s的0~len出发(为什么只用从0~len出发呢,因为words的每个单词长度是确定的,这样其实从字符串0~len出发每次递增len长度,你会发现全都会访问到。),每次递增单词长度就为len,窗口大小维护为words的大小,这样当不连续的时候,就消减窗口,直到连续为止,这样当窗口大小又等于words的大小的时候就可以把头放入vector中。

Code

class Solution {
public:
    vector<int> findSubstring(string s, vector<string> &words) {
        if (s.empty() || words.empty())
            return vector<int>();
        vector<int> ans;
        int m = words[0].size(), n = s.size(), sum = words.size(), nn = n - m;
        unordered_map<string, int> count;
        for (const string &w:words)count[w]++;
        for (int i = 0; i < m; i++) {
            unordered_map<string, int> mp;
            int left = i, cnt = 0;
            for (int j = i; j <= nn; j+=m) {
                string tmp = s.substr(j, m);
                if (!count.count(tmp)) {
                    cnt = 0;
                    left = j+m;
                    mp.clear();
                    continue;
                }
                mp[tmp]++;
                if (mp[tmp] <= count[tmp])cnt++;
                else {
                    while (mp[tmp] > count[tmp]) {
                        string stmp = s.substr(left, m);
                        mp[stmp]--;
                        left += m;
                        cnt--;
                    }
                
                    cnt++;
                }
                if (cnt == sum) {
                    ans.push_back(left);
                }
            }
        }
        return ans;
    }
};
一文入门车载以太网:从原理到实战,看完就能上手开发 新手选型遵循“适配场景、兼顾成本、优先车规”的原则:普通场景(车载娱乐、普通诊断):100Base-T1 PHY(LAN8742)+ 非TSN交换机(SJA1105)+ STM32H743 ECU + 普通STP线缆;ADAS场景:1000Base-T1 PHY(TJA1101)+ TSN交换机(SJA1110)+ NXP S32G399 ECU + 高抗干扰STP线缆;避免过度选型:如普通娱乐场景无需选择TSN交换机、ASIL-B级芯片,否则会增加成本; 阅读详情

相关推荐

Truncated SVD for faster dection

Truncated SVD方法最先被提出在《Fast R-CNN》论文中,用于降低全连接层的运算量,提升模型的速度。在卷积神经网络的分类任务中,FC层的运算速度远远大于CONV层,而在Fast R-CNN的目标检测任务中,大多数的运算时间发生在FC 的运算中,是因为针对每张输入图片,只进行一次卷积操作,而要对生成的Region Proposal,大致两千个,进行全连接操作进行分类与Bounding...

weixin_35747666的博客 1535

Leetcode #49 Anagrams

Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. Difficulty: Medium 典型的hash,用map做。 在此我得到了一个小启发,使用sort()重新排列字母比较会比较简单。 v

每天都要有进步 353

【rt-thread实战解析】从零到一:基于STM32与LAN8720A的以太网驱动配置全流程

本文详细解析了在RT-Thread操作系统上为STM32F407配置LAN8720A以太网驱动的全流程。从硬件连接自查、CubeMX工程创建、PHY复位函数编写,到RT-Thread工程配置、LwIP协议栈集成,最后进行实际上电测试与网络调试,手把手指导开发者避开常见陷阱,实现稳定的以太网连接。

m2n3b4v5c6的博客 526

LeetCode-Group Anagrams

Given an array of strings, group anagrams together. For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return: [ ["ate", "eat","tea"], ["nat","tan"], ["bat"] ] Note: ...

weixin_34237596的博客 135

告别降级CUDA!手把手教你为RTX 50系显卡(5080/5090)配置专属的PyTorch Nightly + vLLM推理环境

本文详细指导如何为RTX 50系显卡(5080/5090)配置专属的PyTorch Nightly和vLLM推理环境,特别针对CUDA 12.8的适配问题提供解决方案。从硬件准备到源码编译,再到模型部署与性能调优,手把手教你构建高效AI推理系统。

weixin_33724046的博客 409

【PT+Arduino+OneNET基础教程1】PacketTracer中MCU芯片编程教程(零基础入门)

PacketTracer软件内MCU芯片编程入门 零基础

ChloeMax_的博客 9456

Anagrams

Anagrams Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 题目:理解anagrams的意思,

u012158672的专栏 481

嵌入式人工智能(18-基于树莓派4B的继电器JQC-3FF-S-Z)

本次实验的输出端控制不会接强电,就用LED灯来模拟即可,由于我那个流水灯模块是低电平点亮,所以接线稍有不同,同学们注意。只有继电器吸合,输出端的NO与COM就导通,给LED低电平然后就亮,否则继电器断开,NO与COM不导通,LED不亮。原理都一样,控制电流的接通与断开,这个模块看起来还简单点,毕竟还有2个指示灯,下端IN如接上电源红灯亮,继电器动作有电流通过绿灯亮。在实际应用中,可以通过编程控制继电器的通断状态,例如在Python编程中设置继电器模块的通断时间,从而实现自动化控制。

u010152658的博客 2531

How to Determine if Two Words Are Anagrams of Each Other in C# (转)

How to Determine if Two Words Are Anagrams of Each Other in C# By Sai Bagewadi on May 19, 2015 ms Two words are said to be Anagrams of each other if they share the same set of letters to form

hicarl的专栏 950

Playwright实战:打造绕过全维度前端反爬的无头浏览器爬虫(2026实战版)

Playwright绕过前端反爬的核心是模拟真实:真实的设备指纹、真实的行为轨迹、真实的网络请求;2025年前端反爬的核心检测维度是设备指纹+行为轨迹+网络请求,三者缺一不可;实战中需重点关注WebGL/Canvas指纹模拟、行为随机性、请求头完整性,这是绕过反爬的关键;性能优化需以“不触发反爬”为前提,优先选择浏览器复用、资源拦截,而非无限制并发。

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 2094

【数据集8】全球人口数据WorldPop详解

全球人口数据WorldPop详解

WW、forever的博客 7882

leetcode-30. Substring with Concatenation of All Words

leetcode-30. Substring with Concatenation of All Words题目: 这题其实算不上难题。至少是难题中比较简单的。 基本思路就是由于words中的String的长短是一样的,那这样就有比较简单的解法,就是用i从0开始向右移动,找substring(0,words[0],length())是否包含在words中就好。当然针对每个i都需要新建一个hashset,并将w

千念飞羽的专栏 554

UNet 改进(19):与ASPP结合的图像分割模型

这个代码实现了一个改进版的UNet模型,在传统的UNet架构基础上,引入了ASPP模块来增强模型捕获多尺度上下文信息的能力。​​UNet主干网络​​:经典的编码器-解码器结构,带有跳跃连接​​ASPP模块​​:位于瓶颈层,用于提取多尺度特征。

2401_82355416的博客 879

Leetcode1-100: 30. Substring with Concatenation of All Words

Leetcode1-100: 30. Substring with Concatenation of All Words问题描述解题思路代码实现implement 1 brute forceimplement 2 sliding window分析 问题描述 You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(

Y123iwantit的博客 272

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

LeetCode代码分析——30. Substring with Concatenation of All Words

LeetCode代码分析——30. Substring with Concatenation of All Words You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that...

huanglong0438的专栏 385

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

leetcode 30 Substring with Concatenation of All Words

Substring with Concatenation of All Words

GadyPu的专栏 451

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

[leetcode]30. Substring with Concatenation of All Words

自己写的乱七八糟的代码,无法解决s或words里有重复的问题 class Solution { public List&lt;Integer&gt; findSubstring(String s, String[] words) { List&lt;Integer&gt; list=new ArrayList&lt;Integer&gt;(); ...

Belle_Chou的博客 94

30. Substring with Concatenation of All Words - 力扣(LeetCode)

题目: 给定一个字符串s和一些长度相同的单词words。找出 s 中恰好可以由words 中所有单词串联形成的子串的起始位置。 注意子串要与words 中的单词完全匹配,中间不能有其他字符,但不需要考虑words中单词串联的顺序。 示例 1: 输入: s = "barfoothefoobarman", words = ["foo","bar"] 输出:[0,9]...

qq_29760277的博客 164

leetcode (30) Substring with Concatenation of All Words

原题链接:https://leetcode.com/problems/substring-with-concatenation-of-all-words/ 题目:30. Substring with Concatenation of All Words You are given a string, s, and a list of words, words, that are

菜鸟很菜的专栏 433

LeetCode 30 Substring with Concatenation of All Words 解法为Hashmap

Substring with Concatenation of All Words You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concaten

gx262091291的专栏 376

LeetCode(30)--Substring with Concatenation of All Words

转载自博客 LeetCode30———–Substring with Concatenation of All Words 看题目都有些费劲,要不是看了几组Custom Testcase才知道题意,顺便说一句Custom Testcase这个功能在调试的时候真的非常实用。 题目意思: 给定一个字串不妨称为母串S,和一组单词,他们保存在一个string的顺序容器Words中,并且满足一个条件,

ruosiliu_123的博客 299

LeetCode-30. Substring with Concatenation of All Words

前言 突然一周没刷过题了,有点时间就刷一下题吧,顺便把解题思路记录下来,一个是自己做笔记,一个是给大家一点思路。前面的二十多道以后有时间再补吧,现在刷到了30题,就从这个开始吧! 问题描述 解法一 首先是一种暴力解法,直接遍历所有可能的list,然后看这个list是否满足要求,在验证该list是否满足要求 的时候需要使用到类似hash的数据结构,保证words中的每个元素都被用到过一次,由于其中...

Catcher 270

[leetcode]-30-Substring with Concatenation of All Words

题目: You are given a string,s, and a list of words,words, that are all of the same length. Find all starting indices of substring(s) insthat is a concatenation of each word inwordsexactly once a...

ljh0302的专栏 617

leetcode 30. Substring with Concatenation of All Words

You are given a string,s, and a list of words,words, that are all of the same length. Find all starting indices of substring(s) insthat is a concatenation of each word inwordsexactly once and wi...

cmy203的博客 149

[LeetCode30]Substring with Concatenation of All Words

Substring with Concatenation of All Words You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenatio

coder 进阶的专栏 945
上一篇: LeetCode 10. Regular Expression Matching
下一篇: LeetCode 44. Wildcard Matching
WX_ming
博客等级 码龄9年 5粉丝 146原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值