Substring with Concatenation of All Words
寻找包含所有单词的子字符串,按照人工寻找的方式有如下代码,对于每一个index依次进行比对,该方案通过了167/169,最后两个字符串超时了

class Solution(object):def findSubstring(self, s, words):""":type s: str:type words: List[str]:rtype: List[int]"""ls = len(s)lw = len(words)if not ls or not lw:return -1wl = len(words[0])res = []dic = {}for word in words:if not dic.has_key(word):dic[word] = 1else :dic[word] += 1i = 0 while i + wl <= ls:if s[i: i+wl] not in words:i += 1else :tdic = {}tdic[s[i: i+wl]] = 1j = 1while i + j * wl + wl <= ls:if s[i + j * wl: i + j * wl + wl] not in words:#i = i + 1breakelse :if not tdic.has_key(s[i + j * wl: i + j * wl + wl]):tdic [s[i + j * wl: i + j * wl + wl]] = 1j += 1else :if tdic[s[i + j * wl: i + j * wl + wl]] < dic[s[i + j * wl: i + j * wl + wl]]:tdic [s[i + j * wl: i + j * wl + wl]] += 1j += 1else :breakflag = 0for key in dic:if not tdic.has_key(key):flag = 1breakelif tdic[key] != dic[key]:flag = 1breakif not flag :res += [i]i += 1return res

研究一下讨论区的其他算法:

class Solution(object):def findSubstring(self, s, words):""":type s: str:type words: List[str]:rtype: List[int]"""if not s or not words or not words[0]:return []n = len(s)k = len(words[0])t = len(words) * kreq = {}for w in words:req[w] = req[w] + 1 if w in req else 1ans = []for i in range(min(k, n - t + 1)):self._findSubstring(i, i, n, k, t, s, req, ans)return ansdef _findSubstring(self, l, r, n, k, t, s, req, ans):curr = {}while r + k <= n:w = s[r:r + k]r += kif w not in req:l = rcurr.clear()else:curr[w] = curr[w] + 1 if w in curr else 1while curr[w] > req[w]:curr[s[l:l + k]] -= 1l += kif r - l == t:ans.append(l)

这个算法实现的主要是一个滑动窗口,当word[0],word[1],word[0]出现时,向后滑一个从word[1],word[0]以及往后开始重新记,其中假设word[0]在words中是唯一的,不唯一时就在超出数量时滑动。为了防止每次滑动一个单词长度遗漏答案,特意将开始的word长度字符分别做头。

AC算法看起来正是用来解决此类问题的,AC算法详解

LeetCode刷题(19)相关推荐

  1. C#LeetCode刷题-程序员面试金典

    本文由 比特飞 原创发布,欢迎大家踊跃转载. 转载请注明本文地址:C#LeetCode刷题-程序员面试金典 | .Net中文网. C#LEETCODE刷题概述 概述 所有LeetCode程序员面试金典 ...

  2. C#LeetCode刷题-剑指Offer

    本文由 比特飞 原创发布,欢迎大家踊跃转载. 转载请注明本文地址:C#LeetCode刷题-剑指Offer | .Net中文网. C#LEETCODE刷题概述 概述 所有LeetCode剑指Offer ...

  3. C#LeetCode刷题-二叉搜索树

    二叉搜索树篇 # 题名 刷题 通过率 难度 220 存在重复元素 III 19.3% 中等 315 计算右侧小于当前元素的个数 31.9% 困难 327 区间和的个数 29.5% 困难 352 将数据 ...

  4. C#LeetCode刷题-排序

    排序篇 # 题名 刷题 通过率 难度 56 合并区间 31.2% 中等 57 插入区间 30.4% 困难 75 颜色分类 48.6% 中等 147 对链表进行插入排序 50.7% 中等 148 排序链 ...

  5. C#LeetCode刷题-贪心算法

    贪心算法篇 # 题名 刷题 通过率 难度 44 通配符匹配 17.8% 困难 45 跳跃游戏 II 25.5% 困难 55 跳跃游戏 30.6% 中等 122 买卖股票的最佳时机 II C#LeetC ...

  6. C#LeetCode刷题-动态规划

    动态规划篇 # 题名 刷题 通过率 难度 5 最长回文子串 22.4% 中等 10 正则表达式匹配 18.8% 困难 32 最长有效括号 23.3% 困难 44 通配符匹配 17.7% 困难 53 最 ...

  7. C#LeetCode刷题-二分查找​​​​​​​

    二分查找篇 # 题名 刷题 通过率 难度 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)-该题未达最优解 30 ...

  8. C#LeetCode刷题-双指针

    双指针篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串 24.5% 中等 11 盛最多水的容器 43.5% 中等 15 三数之和 16.1% 中等 16 最接近的三数之和 34.6% 中等 ...

  9. C#LeetCode刷题-数学

    数学篇 # 题名 刷题 通过率 难度 2 两数相加 29.0% 中等 7 反转整数 C#LeetCode刷题之#7-反转整数(Reverse Integer) 28.6% 简单 8 字符串转整数 (a ...

  10. C#LeetCode刷题-链表

    链表篇 # 题名 刷题 通过率 难度 2 两数相加   29.0% 中等 19 删除链表的倒数第N个节点   29.4% 中等 21 合并两个有序链表 C#LeetCode刷题之#21-合并两个有序链 ...

最新文章

  1. Numpy学习笔记(下篇)
  2. 百度地图- - - 鹰眼轨迹- - - -实时定位
  3. 如何测试 SAP OData的filter功能
  4. JVM内存结构|虚拟机栈
  5. 程序员修神之路--它可能是分布式系统中最重要的枢纽
  6. 纯CSS3文字Loading动画特效
  7. 使用urllib2库实现有道翻译
  8. php 增加mysql 索引,【PHP】为什么 MySQL 添加索引后就可以提高查询速度
  9. cocos2d-x初探学习笔记(1)--HelloWorld .
  10. python字符串转换为数字_Python不使用int()函数把字符串转换为数字的方法
  11. Stata 中的向量自回归模型(VAR)
  12. 斐波那契堆(Fibonacci heap)原理详解
  13. 2017年秋季校招前端面经(百度,腾讯,网易,华为,乐视等)
  14. 产权登记在未成年子女名下,离婚时应如何处理
  15. java实现别踩白块儿,jQuery实现别踩白块儿网页版小游戏
  16. 程序员应该学什么语言
  17. 九步!让你从零基础学习python成为一名开源程序员!
  18. Mob研究院 |2019互联网医疗行业洞察
  19. ncr管理系统_完全拆解小米智能电动车【图解】
  20. android模拟器不能运行 控制台显示 VCPU shutdown request

热门文章

  1. DB2 SQLSTATE 消息
  2. asp.net ajax 1.0中detailview与updatepanel混合使用的例子
  3. http://dev2dev.bea.com.cn/bbs/thread.jspa?forumID=122threadID=9172tstart=0
  4. SpringMVC @ResponseBody 406
  5. 13.python中的字典
  6. Android 4.0 NDK Updated
  7. java 用文件对话框打开文件
  8. coursera 视频总是缓冲或者无法观看,有什么方法解决?
  9. react 解决 setState 异步问题
  10. RLException: XXX is neither a launch file in package XXX nor is XXX a launch file name问题解决