30. 串联所有单词的子串 Golang实现
题目描述:
给定一个字符串 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] 也是可以的。
题目分析:
题目希望找到s中字符数组的字符串的所有排列组合的开始下标。注意题目提到的对所有条件的标示,字符数组的字符串的长度的长度是相同的。设置这个条件自然是有用的,如果字符串的长度不相同,那么只能找到所有可能的字符串,再在s中去匹配,但是长度相同,就可以使用固定大小的滑动窗口来解决,只要连续出现的字符串能覆盖掉所有words,就是找到成功。 如何判断单词是否出现==>通过词频进行统计。
输入: 字符串 s,字符串数组 words
输出: words中所有字符的排列组合在s中开始的下标
条件: words中所有字符串的长度相同 1 <= s.length <= 10⁴
点击查看代码
func findSubstring(s string, words []string) []int {
if len(words) == 0 || len(s) < len(words)*len(words[0]) {
return []int{}
}
wordsCount := make(map[string]int)
for _, word := range words {
wordsCount[word]++
}
wordLen := len(words[0])
totalWords := len(words)
res := []int{}
for i := 0; i < wordLen; i++ {
left := i
count := 0
windowCount := make(map[string]int)
for right := i; right <= len(s)-wordLen; right += wordLen {
word := s[right : right+wordLen]
if _, exists := wordsCount[word]; exists {
windowCount[word]++
count++
for windowCount[word] > wordsCount[word] {
leftWord := s[left : left+wordLen]
windowCount[leftWord]--
count--
left += wordLen
}
if count == totalWords {
res = append(res, left)
}
} else {
windowCount = make(map[string]int)
count = 0
left = right + wordLen
}
}
}
return res
}
解法二:
依照题目的意思,先得到所有单词的排列组合,再将每个单词放入s中寻找是否存在。
30. 串联所有单词的子串 Golang实现的更多相关文章
- Java实现 LeetCode 30 串联所有单词的子串
30. 串联所有单词的子串 给定一个字符串 s 和一些长度相同的单词 words.找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置. 注意子串要与 words 中的单词完全匹配, ...
- [LeetCode] 30. 串联所有单词的子串
题目链接: https://leetcode-cn.com/problems/substring-with-concatenation-of-all-words/ 题目描述: 给定一个字符串 s 和一 ...
- Leetcode 30 串联所有单词的子串 滑动窗口+map
见注释.滑动窗口还是好用. class Solution { public: vector<int> findSubstring(string s, vector<string> ...
- leetcode 30. 串联所有单词的子串 【时间击败 90.28%】 【内存击败 97.44%】
这道题让我从早做到晚-3--- 设len=words[0].length(). 一开始我按照words的顺序扩大区间,发现这样就依赖words的顺序.之后改成遍历s的所有长度为len*words.le ...
- [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 sta ...
- [LeetCode] 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 sta ...
- 【LeetCode-面试算法经典-Java实现】【030-Substring with Concatenation of All Words(串联全部单词的子串)】
[030-Substring with Concatenation of All Words(串联全部单词的子串)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Yo ...
- 【LeetCode 30】串联所有单词的子串
题目链接 [题解] 开个字典树记录下所有的单词. 然后注意题目的已知条件 每个单词的长度都是一样的. 这就说明不会出现某个字符串是另外一个字符串的前缀的情况(除非相同). 所以可以贪心地匹配(遇到什么 ...
- leetcode30 串联所有单词的子串
先对words中的单词排列组合,然后对s滑窗操作:部分样例超时,代码如下: class Solution { public: vector<int> findSubstring(strin ...
- Leetcode 30.与所有单词相关联的子串
与所有单词相关联的字串 给定一个字符串 s 和一些长度相同的单词 words.在 s 中找出可以恰好串联 words 中所有单词的子串的起始位置. 注意子串要与 words 中的单词完全匹配,中间不能 ...
随机推荐
- Day 1 - 二分
整数二分 我们可以做到每次排除一半的答案,时间复杂度 \(O(\log n)\). long long l = L, r = R; while(l <= r) { long long mid = ...
- 靶机: hard_socnet2
靶机: hard_socnet2 准备 靶机:https://download.vulnhub.com/boredhackerblog/hard_socnet2.ova MD5 验证: 9d6bed1 ...
- 关于IE11点击的select框需要点击两次才能展开option选择框
需求:select是一个级联选择框,点击select框之后向后台请求,然后把请求的数据展示出来. 问题:绑定onclick或者onfocus的时候,需要点击select框两次,才能将option选择框 ...
- 「图论」Bron-kerbosch算法
7.21晚上加赛 T2.七负我,做这题找到了性质发现需要求最大团,不会,爆搜,打假了,赛后改,对了,但时间复杂度大爆炸,看下发题解,有这么一句话:于是学习了一下. Bron-kerbosch算法-求图 ...
- 20+前端常用的vscode插件(总结推荐)
本篇文章给大家总结分享20多个前端常用的vscode插件.有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助. 1. vscode 简介vscode是微软开发的的一款代码编辑器,就如官网上 ...
- php环境-2024年3月19日
laravel 6[laravel的orm比其他框架的好用,可以写很少的代码就能完成] php 7.4 mysql 5.7 centos7 redis jwt 队列(laravel的redis队列,或 ...
- 用IO多路复用实现 nginx 静态资源代理(C/Java/Golang)
用IO多路复用实现 nginx 静态资源代理(C/Java/Golang) 效果展示 代理 HTML 代理图片 注意, 静态资源代理基于 HTTP, 可以了解上一篇文章: 几十行代码使用TCP简单实现 ...
- 5、Git之版本号
5.1.概述 每一次提交,Git 都会生成相关的版本号:每个版本号由 40 位 16 进制的数字组成. 这 40 位 16 进制的数字,是根据提交的内容,通过 SHA-1 算法计算出来的. 版本号具体 ...
- 【Git】GithubDesktop 忽略文件无法忽略BUG
问题描述: 从仓库拉取的[.gitignore]忽略配置文件,在项目跑起来之后会生成诸多.idea文件,target打包文件 一开始没有忽略,但是发现使用GD配置之后忽略无效: 解决办法: 做一次随便 ...
- 【Layui】05 选项卡 Tabs
文档位置: https://www.layui.com/doc/element/tab.html 案例演示: <div class="layui-tab"> <u ...