题目描述

给定一个字符串 s 和一些长度相同的单词 words。在 s 中找出可以恰好串联 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 = "wordgoodstudentgoodword",words = ["word","student"]
Output: []

题目链接

解题代码

from collections import defaultdictclass Solution:def findSubstring(self, s, words):""":type s: str:type words: List[str]:rtype: List[int]"""res = []if len(words) == 0 or len(s) < len(words[0]) * len(words):return resstr_len = len(s)word_len = len(words[0])total_len = len(words) * word_lenmap_dict = defaultdict(int)cur_dict = defaultdict(int)for word in words:map_dict[word] += 1for start in range(0, str_len - total_len + 1):end = startwhile end < start + total_len:substr = s[end: end + word_len]cur_dict[substr] += 1# 如果当前dict中还有之前的没有的,或者值比至之前的大都终止if cur_dict[substr] > map_dict[substr]:breakend += word_lenif end == start + total_len:res.append(start)cur_dict.clear()return res

主要还是用滑动窗口法解决问题,有关滑动窗口法的详解,请看这里
完整源码在这里

leetcode 30. Substring with Concatenation of All Words 与所有单词相关联的字串 滑动窗口法相关推荐

  1. 030 Substring with Concatenation of All Words 与所有单词相关联的字串

    给定一个字符串 s 和一些长度相同的单词 words,找出 s 与 words 中所有单词(words 每个单词只出现一次)串联一起(words中组成串联串的单词的顺序随意)的字符串匹配的所有起始索引 ...

  2. LeetCode(30):与所有单词相关联的字串

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

  3. LeetCode 30. Substring with Concatenation of All Words

    LeetCode 30. Substring with Concatenation of All Words Solution1: 转载自:http://www.cnblogs.com/grandya ...

  4. **LeetCode 30. Substring with Concatenation of All Words

    https://leetcode.com/problems/substring-with-concatenation-of-all-words/ 滑动窗口法.解析看这里: http://www.2ct ...

  5. [剑指offer]面试题第[48]题[Leetcode][JAVA][第3题][无重复字符的最长字串][滑动窗口][HashSet/Map]

    [问题描述][第3题][无重复字符的最长字串] 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度.示例 1:输入: "abcabcbb" 输出: 3 解释: 因为无重 ...

  6. LeetCode 3.无重复字符的最长字串(滑动窗口)

    题目描述 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "a ...

  7. leetcode Longest Substring with At Most Two Distinct Characters 滑动窗口法

    题目解析 代码如下 题目解析 这一题是一道会员的题目,题目介绍如下: Given a string, find the length of the longest substring T that c ...

  8. leetcode 3. Longest Substring Without Repeating Characters 最长非重复子串的长度 滑动窗口法

    题目链接 根据我们之前介绍的滑动窗口法的解法: 滑动窗口法详解 leetcode 438. Find All Anagrams in a String 滑动窗口法 这题,我们不难解决,使用之前的模板. ...

  9. 【LeetCode】无重复字符的最长子串【滑动窗口法】

    给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc&qu ...

最新文章

  1. 面试环节:在浏览器输入 URL 回车之后发生了什么?(超详细版)
  2. 趣谈深度学习核心----激活函数
  3. openCv java Mat和MatOfByte的之间的相互转换 (4)
  4. CMU研究人员提出一种新的深部脑刺激方法,可延长帕金森病治疗效果
  5. c 没有mysql名称空间_MySQL如何删除#sql开头的临时表
  6. .Net缓存小结(中)
  7. 可持久化汇总(讲解+题目)
  8. unity3d教程游戏包含的一切文件导入资源
  9. HDU 4383 To The Moon 解题报告
  10. Java中string中hashcode_Java String中的hashCode实现
  11. ListIterator和Iterator的区别
  12. php有空语句吗?,php是空还是空?
  13. 计算机操作系统|汤小丹|第四版|习题答案(五)
  14. python之t分布
  15. 用gitee搭建自己的博客网站
  16. PHP一句话木马后门
  17. javascript中的交互效果
  18. 什么是 Android Jetpack?
  19. android开发播放声音文件
  20. 我的世界java局域网存档位置_网易版我的世界局域网联机存档怎么转移 如何将局域网存档转到租赁服...

热门文章

  1. 使用JS/Jquery获得父窗口的几个方法(笔记)
  2. 2022-2028年中国金融安防行业深度调研及投资前景预测报告
  3. TF.VARIABLE和TENSOR的区别(转)
  4. LeetCode简单题之判断能否形成等差数列
  5. LeetCode简单题之分糖果 II
  6. LeetCode简单题之反转单词前缀
  7. Paddle广播 (broadcasting)
  8. NVIDIA TensorRT:可编程推理加速器
  9. 怎么卸载云骑士装机大师
  10. 量子力学在计算机上的应用,量子力学在医学科学中的应用