[Leetcode][Python]30: Substring with Concatenation of All Words
# -*- 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的更多相关文章
- 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 ...
- 【一天一道LeetCode】#30. Substring with Concatenation of All Words
注:这道题之前跳过了,现在补回来 一天一道LeetCode系列 (一)题目 You are given a string, s, and a list of words, words, that ar ...
- 【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 ...
- leetcode python 030 Substring with Concatenation of All Words
## 您将获得一个字符串s,以及一个长度相同单词的列表.## 找到s中substring(s)的所有起始索引,它们只包含所有单词,## eg:s: "barfoothefoobarman&q ...
- LeetCode - 30. Substring with Concatenation of All Words
30. Substring with Concatenation of All Words Problem's Link --------------------------------------- ...
- 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 ...
- [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 ...
- [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 ...
- 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 ...
随机推荐
- 关于 <video> 的兼容性测试
测试浏览器 Firefox: 33.0.1 Chrome: 38.0.2125.111 m Safari: 5.1.7 IE: 9 和 10(6,7,8不考虑) 经测试:FF.Chrome.Safar ...
- HttpSQS
http://goaheadtw.iteye.com/blog/1669682 http://zyan.cc/httpsqs/7/1/
- ubuntu_安装aptana3
下面记录下偶怎么安装aptana3(aptana2应该也适用). 安装java运行时,偷看这里 说明:实际上偶并没有执行这步,因为发现在安装aptana3之前 java的运行时已经安装过了. 貌似是安 ...
- 2014第11周四Eclipse开发问题记
今天开发中eclipse工具使用上又学到几点: 1.去除代码空行:在Find输入框中输入:^\s*\n然后替换为空即可: 2.eclipse插件的加载:对于单一个jar文件的插件,直接放在plugin ...
- #include <locale.h> #include <locale>
C C++ C 1 setlocale setlocale,本函数用来配置地域的信息,设置当前程序使用的本地化信息. #include <stdio.h> #include <std ...
- Python实现CGI环境
要想安装Python的CGI环境,就继续往下看吧. 首先,要确定apache服务可以使用CGI服务. 打开apache的配置文件,设置如图. 在启动的apache服务的系统目录下,创建目录如/User ...
- tky项目第①个半月总结
增加tky项目开发组已经有半个月了,这半个月一直是伴随着加班度过,学习了不少东西,也有不少抱怨,这些都是宝贵的工作经验体会.有必要在此好好总结一下. 我是中途加进这个项目的.也就是说.组内其它人员已经 ...
- 蓝桥杯算法训练<二>
一.最小乘积(基本型)[这个题需要认真阅读试题,内容量较大,刚开始的时候,由于练习系统上给出的输入输出的格式有问题,没看懂,最后在MikCu的博客上看到了正确的格式,参考了代码,最终得到正确的结果.为 ...
- 蓝桥杯算法训练<一>
一.图形显示 此题虽然简单,但是需啊哟注意的是,每个“*”后边有一个空格] 问题描述 编写一个程序,首先输入一个整数,例如5,然后在屏幕上显示如下的图形(5表示行数): * * * * * * * * ...
- iOS百度推送的基本使用
一.iOS证书指导 在 iOS App 中加入消息推送功能时,必须要在 Apple 的开发者中心网站上申请推送证书,每一个 App 需要申请两个证书,一个在开发测试环境下使用,另一个用于上线到 App ...