# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com'
30: Substring with Concatenation of All Words
https://oj.leetcode.com/problems/substring-with-concatenation-of-all-words/
You are given a string, S, and a list of words, L, that are all of the same length.
Find all starting indices of substring(s) in S that is a concatenation of each word in L
exactly once and without any intervening characters. For example, given:
S: "barfoothefoobarman"
L: ["foo", "bar"] You should return the indices: [0,9].
(order does not matter). ===Comments by Dabay=== 生成一个hash表来记录每个词在L中出现的次数。
扫描要检查的字符串S,检查以这个字母开始的 长度为L总长度 的字符串,
如果小词在hash表中,值减一;如果最后每一个hash值都是0,说明正好全部匹配完,加入到结果。
重新初始化hash表,进行下一次检查。
''' class Solution:
# @param S, a string
# @param L, a list of string
# @return a list of integer
def findSubstring(self, S, L):
d = {}
for x in L:
if x not in d:
d[x] = 1
else:
d[x] += 1
res = []
word_length = len(L[0])
i = 0
while i < len(S) - word_length * len(L) + 1:
j = i
dd = dict(d)
while j < i + word_length * len(L):
word = S[j:j+word_length]
if word in dd:
dd[word] -= 1
if dd[word] < 0:
break
else:
break
j += word_length
else:
res.append(i)
i += 1
return res def main():
s = Solution()
string = "aaa"
dictionary = ["a", "b"]
print s.findSubstring(string, dictionary) if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)

[Leetcode][Python]30: Substring with Concatenation of All Words的更多相关文章

  1. LeetCode HashTable 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 ...

  2. 【一天一道LeetCode】#30. Substring with Concatenation of All Words

    注:这道题之前跳过了,现在补回来 一天一道LeetCode系列 (一)题目 You are given a string, s, and a list of words, words, that ar ...

  3. 【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 ...

  4. leetcode python 030 Substring with Concatenation of All Words

    ## 您将获得一个字符串s,以及一个长度相同单词的列表.## 找到s中substring(s)的所有起始索引,它们只包含所有单词,## eg:s: "barfoothefoobarman&q ...

  5. LeetCode - 30. Substring with Concatenation of All Words

    30. Substring with Concatenation of All Words Problem's Link --------------------------------------- ...

  6. leetcode面试准备: Substring with Concatenation of All Words

    leetcode面试准备: Substring with Concatenation of All Words 1 题目 You are given a string, s, and a list o ...

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

  8. [LeetCode] 30. Substring with Concatenation of All Words 解题思路 - Java

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

  9. leetCode 30.Substring with Concatenation of All Words (words中全部子串相连) 解题思路和方法

    Substring with Concatenation of All Words You are given a string, s, and a list of words, words, tha ...

随机推荐

  1. listView 单选实现

    上一篇知道可以使用android自带的listview的chiocemode的单选模式实现.但那个布局是系统自带的checkedTextView,有时候我们需要自己实现布局,那么下面我们开始实现   ...

  2. 《UNIX网络编程》TCP客户端服务器:并发、消息回显

    经过小小改动,把前面基础的例子做出一点修改. 并发服务器,服务器每accept一个请求就fork()一个新的子进程. 编译运行方法同前一篇. /*client_tcp.c*/ #include < ...

  3. ASP.NET页面继承关系

    用过ASP.NET(以下简称ASP)的都知道ASP以一种Code Behind的方式给咱展现了一种类似Winform的开发模型,同样也是以"事件触发"的方式进行各种请求处理.其中A ...

  4. rabbitmq问题之HTTP access denied: user 'guest' - User can only log in via localhost

    问题: 昨天安装rabbitmq(3.3.4版本)服务,并启用rabbitmq_management插件去管理rabbitmq服务,但是在访问管理界面使用guest用户登录时出现login faile ...

  5. linux md5 加密字符串和文件方法

    linux md5 加密字符串和文件方法 MD5算法常常被用来验证网络文件传输的完整性,防止文件被人篡改.MD5全称是报文摘要算法(Message-Digest Algorithm 5),此算法对任意 ...

  6. 转载文章:Windows Azure 基础结构服务上的 Microsoft Dynamics NAV 和 Microsoft Dynamics GP!

    Windows Azure 基础结构服务(虚拟机和虚拟网络)可提供按需基础结构,该基础结构可进行伸缩以适应不断变化的业务需求.无论您是在虚拟机中创建新应用程序,还是运行现有应用程序,我们都将按分钟收费 ...

  7. HttpHelps类,用来实现Http访问,Post或者Get方式的,直接访问,带Cookie的,带证书的等方式,可以设置代理

    原文地址:http://blog.csdn.net/cdefg198/article/details/8315438 万能框架:http://www.sufeinet.com/forum.php?mo ...

  8. Spring、AOP详解

    如何配置AOP查看:Spring.Hello AOP 1.对于拦截规则@Pointcut的介绍: @Pointcut("execution (* cn.raffaello.service.. ...

  9. Linux oracle 11g r2 安装前检查及安装

    Linux环境配置 [c-sharp] view plaincopy OS:Fedora 15 DB:Oracle 11gR2 将Oracle安装到home/oracle_11目录 配置过程:本文来自 ...

  10. 解密yii中CModule::_components和CModule::_componentConfig

    array CModule::_components 所有组件对象(CComponent的子类)将作为键值存在该数组中, 键名是定义该组件时使用的键名.例如: protected function r ...