# -*- 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. 关于 table-layout 属性

    我们经常会使用table,并且去手动的设置它的宽度,比如: <table> <tr> <td class="left">...</td&g ...

  2. Python成长之路第一篇(4)_if,for,while条件语句

    有了以上的基本基础,已经上面写的几个小练习,大家肯定有很多的不满,比如查询为什么查询一次就退出了呢?下面我们来学习条件语句 一.万恶的加号 以前我们在print的时候如果要加上变量都有是使用+来作为连 ...

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

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

  4. AndroidUI 视图动画-缩放动画效果 (ScaleAnimation)

    放动画效果,可以使用ScaleAnimation: <Button android:id="@+id/btnScale2" android:layout_width=&quo ...

  5. html中给图片添加热点

    <img src="images/index/top1.jpg" width="248" height="512" usemap=&q ...

  6. magento中的各种form标签

    1. Text Field    $fieldset->addField('title', 'text', array(          'label'     => Mage::hel ...

  7. 设计模式六大原则——迪米特法则(LoD)

    1.背景 在图书馆借书.刚開始的时候,直接跑到对应的楼层去,到里面去转,去找要借的书,在里面溜达半天才干找到:后来知道图书馆有一个电脑查询处.然后直接在电脑上输入想要借的书,电脑就会显示你想要借的书的 ...

  8. JFinal开发web项目出现故障小记

    导读 作为中国优秀的开源项目之中的一个JFinal有着极速开发的优点,是中小型应用开发的首选.在导师的建议下.我使用了JFinal来开发一个Java服务端应用,官方教程非常easy.就几十页(当然是中 ...

  9. Oracle EBS 如何月结、对账[Z]

    在Oracle系统处理月结业务时,需要遵循一定的操作顺序.这些业务,牵涉到相应的模块,包括:应付模块.采购模块.库存模块.应收模块.薪资模块.固定资产和总帐模块等 在Oracle系统中,总帐模块处于财 ...

  10. Linux下Oracle常见安装错误[Z]

    #./runInstaller之后出现如下的错误信息: RedHat AS5 x86上安装Oracle1020 Exception in thread "main" java.la ...