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 中的单词完全匹配,中间不能 ...
随机推荐
- 入门深度学习和TensorFlow
入门深度学习和TensorFlow时,首先要确保掌握必要的先导知识,然后逐步通过理论和实践相结合的方式深入学习.以下是一个具体的引导例子以及后续的学习计划. 入门深度学习和TensorFlow 1. ...
- OpenGL book note
OpenGL Shading language 4.0vertex shader geometry shader: 格式解释 https://blog.csdn.net/hankern/article ...
- exp解析
1 #pragma once 2 #include <string> 3 #include <functional> 4 #include <type_traits> ...
- 释放资源的方式try-with-resources
1.try-catch-finally 2.try-with-resources 使用方法 try(//这里定义你要使用的资源){} catch(){} 注意:try()里只能存放流对象(资源对象), ...
- Jmeter函数助手34-digest
digest函数用于返回特定哈希算法的加密值. 算法摘要:填入算法,如MD2.MD5.SHA-1.SHA-224.SHA-256.SHA-384.SHA-512 String to be hashed ...
- 【CI/CD】Jenkins 部署前后端项目Demo
前置环境准备: 参考尚硅谷最新发布的Jenkins教程 同样准备了三台服务器: 192.168.124.34 Centos7 8G内存 用于安装GitLab 192.168.124.35 Centos ...
- 【MongoDB】Re02 文档CRUD
三.文档操作(行记录) 不管comment集合是否存在,直接在comment集合中创建一份文档 > db.comment.insert({"articleid":" ...
- 【JS】03 BOM 浏览器对象模型
BOM :Broswer Object Model 浏览器对象模型 核心对象是window对象,window对象又可以操作以下的常见对象: - frames[] 窗口对象数组? 浏览器可以打开多个窗口 ...
- MindSpore 自动微分
代码原地址: https://www.mindspore.cn/tutorial/zh-CN/r1.2/autograd.html MindSpore计算一阶导数方法 mindspore.ops.G ...
- [COCI2013-2014#6] KRUŽNICE 题解
前言 题目链接:洛谷. 题目分析 显然,手模样例发现答案分为以下几个贡献: 所有圆外面的那个大平面,贡献为 \(1\). 每个圆至少被分成一部分,贡献为 \(n\). 如果有一个圆被"拦腰截 ...