题目描述:

给定一个字符串 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实现的更多相关文章

  1. Java实现 LeetCode 30 串联所有单词的子串

    30. 串联所有单词的子串 给定一个字符串 s 和一些长度相同的单词 words.找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置. 注意子串要与 words 中的单词完全匹配, ...

  2. [LeetCode] 30. 串联所有单词的子串

    题目链接: https://leetcode-cn.com/problems/substring-with-concatenation-of-all-words/ 题目描述: 给定一个字符串 s 和一 ...

  3. Leetcode 30 串联所有单词的子串 滑动窗口+map

    见注释.滑动窗口还是好用. class Solution { public: vector<int> findSubstring(string s, vector<string> ...

  4. leetcode 30. 串联所有单词的子串 【时间击败 90.28%】 【内存击败 97.44%】

    这道题让我从早做到晚-3--- 设len=words[0].length(). 一开始我按照words的顺序扩大区间,发现这样就依赖words的顺序.之后改成遍历s的所有长度为len*words.le ...

  5. [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 ...

  6. [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 ...

  7. 【LeetCode-面试算法经典-Java实现】【030-Substring with Concatenation of All Words(串联全部单词的子串)】

    [030-Substring with Concatenation of All Words(串联全部单词的子串)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Yo ...

  8. 【LeetCode 30】串联所有单词的子串

    题目链接 [题解] 开个字典树记录下所有的单词. 然后注意题目的已知条件 每个单词的长度都是一样的. 这就说明不会出现某个字符串是另外一个字符串的前缀的情况(除非相同). 所以可以贪心地匹配(遇到什么 ...

  9. leetcode30 串联所有单词的子串

    先对words中的单词排列组合,然后对s滑窗操作:部分样例超时,代码如下: class Solution { public: vector<int> findSubstring(strin ...

  10. Leetcode 30.与所有单词相关联的子串

    与所有单词相关联的字串 给定一个字符串 s 和一些长度相同的单词 words.在 s 中找出可以恰好串联 words 中所有单词的子串的起始位置. 注意子串要与 words 中的单词完全匹配,中间不能 ...

随机推荐

  1. C# 算术表达式求值(后缀法),看这一篇就够了

    一.种类介绍 算术表达式有三种:前缀表达式.中缀表达式和后缀表达式.一般用的是中缀,比如1+1,前后缀就是把操作符移到前面和后面,下面简单介绍一下这三种表达式. 1.前缀表示法 前缀表示法又叫波兰表示 ...

  2. Odoo 通过Javascript调用模型中自定义方法

    实践环境 Odoo 14.0-20221212 (Community Edition) 代码实现 在js脚本函数中调用模型中自定义方法: this._rpc({ model: 'demo.wizard ...

  3. MySQL 递归查询实践总结

    MySQL复杂查询使用实例 By:授客 QQ:1033553122   表结构设计 SELECT id, `name`, parent_id FROM `tb_testcase_suite` 说明: ...

  4. 假期小结1学习安装VMware以及linux

    学习VMware是一项使我能够创建和管理虚拟机的技能.VMware 是一家知名的虚拟化解决方案供应商,它提供了一系列工具和软件,使我能够在一台物理计算机上创建多个独立的虚拟环境. 首先,我获取了VMw ...

  5. 题解 CF741E Arpa’s abnormal DNA and Mehrdad’s deep interest

    CF741E Arpa's abnormal DNA and Mehrdad's deep interest 记 \(R_{i}\) 表示把 \(T\) 插入在 \(S\) 的第 \(i\) 位后组成 ...

  6. Jmeter边界提取器

    组件路径:HTTP请求->右键添加->后置处理器->边界提取器 用途:边界提取器(Boundary Extractor)是放在Sample请求之后执行的,用于获取左右边界中间的所有字 ...

  7. Fiddler使用界面介绍-右侧面板

    右侧面板是对左侧请求进行解析的面板,点击左侧的请求右侧面板就会出现分析数据 1.Statistics关于HTTP请求的性能 2.Inspectors请求内容,包含请求数据和响应数据 3. AutoRe ...

  8. 7、Git之Github操作

    7.1.注册Github账号 7.1.1.访问官网 Github 官网:https://github.com/ 先访问GitHub的官网首页,点击 sign in (登录),跳转到登录页. 7.1.2 ...

  9. 【Hbase】1.3.1版本安装

    环境: 1核4G内存128G磁盘,三台机器同一个配置 192.168.242.131 192.168.242.132 192.168.242.133 -- Linux Centos7 x64 系统平台 ...

  10. 【转载】 详解nohup /dev/null 2>&1 含义的使用

    原文地址: https://www.jb51.net/article/169837.htm ==================================== 这篇文章主要介绍了详解nohup ...