[leetcode]Substring with Concatenation of All Words @ Python
原题地址: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).
解题思路:使用一个字典统计一下L中每个单词的数量。由于每个单词的长度一样,以题中给的例子而言,可以3个字母3个字母的检查,如果不在字典中,则break出循环。有一个技巧是建立一个临时字典curr,用来统计S中那些在L中的单词的数量,必须和L中单词的数量相等,否则同样break。
代码:
class Solution:
# @param S, a string
# @param L, a list of string
# @return a list of integer
def findSubstring(self, S, L):
words={}
wordNum=len(L)
for i in L:
if i not in words:
words[i]=1
else:
words[i]+=1
wordLen=len(L[0])
res=[]
for i in range(len(S)+1-wordLen*wordNum):
curr={}; j=0
while j<wordNum:
word=S[i+j*wordLen:i+j*wordLen+wordLen]
if word not in words:
break
if word not in curr:
curr[word]=1
else:
curr[word]+=1
if curr[word]>words[word]: break
j+=1
if j==wordNum: res.append(i)
return res
[leetcode]Substring with Concatenation of All Words @ Python的更多相关文章
- LeetCode: Substring with Concatenation of All Words 解题报告
Substring with Concatenation of All Words You are given a string, S, and a list of words, L, that ar ...
- [LeetCode] 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:Substring with Concatenation of All Words (summarize)
题目链接 You are given a string, S, and a list of words, L, that are all of the same length. Find all st ...
- Leetcode 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 startin ...
- [LeetCode] Substring with Concatenation of All Words(good)
You are given a string, S, and a list of words, L, that are all of the same length. Find all startin ...
- Leetcode:Substring with Concatenation of All Words分析和实现
题目大意是传入一个字符串s和一个字符串数组words,其中words中的所有字符串均等长.要在s中找所有的索引index,使得以s[index]为起始字符的长为words中字符串总长的s的子串是由wo ...
- LeetCode()Substring with Concatenation of All Words 为什么我的超时呢?找不到原因了!!!
超时代码 class Solution { public: vector<int> findSubstring(string s, vector<string>& wo ...
- 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][Python]30: Substring with Concatenation of All Words
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 30: Substring with Concatenation of All ...
随机推荐
- 初识thinkphp(2)
thinkphp的url路径的表示格式为 http://ip/tp/public/index.php/模块/控制器/操作 这里url最后的操作就是类里面的函数. 0x01:url访问格式 官方文档中有 ...
- 「BZOJ 4228」Tibbar的后花园
「BZOJ 4228」Tibbar的后花园 Please contact lydsy2012@163.com! 警告 解题思路 可以证明最终的图中所有点的度数都 \(< 3\) ,且不存在环长是 ...
- C#中四种常用集合的运用(非常重要)
C#中4个常用的集合 1.ArrayList ArrayList类似于数组,有人也称它为数组列表.ArrayList可以动态维护,而数组的容量是固定的. 它的索引会根据程序的扩展而重新进行分配和调整. ...
- IBM BR10i阵列卡配置Raid0/Raid1(转)
说明:IBM的阵列卡无论多旧多新操作步骤都基本差不多. RAID1的步骤: 开机自检过程中出现ctrl+c提示,按ctrl+c进入LSI Logic Config Utility v6.10.02.0 ...
- 使用chrome控制台作为日志查看器
很多时候,我们需要查看日志的实时输出,这个时候往往只能选择控制台.我这里的日志框架是serilog,它有一个好处是控制台输出时可以高亮的形式显示其参数结构信息,如下图所示 但也存在许多不方便的地方: ...
- 自定义两行可左右滑动的GridView
效果图: xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...
- [web.config]如何灵活使用配置文件
摘要 在实际项目中,经常遇到比较多的环境,比如开发环境,测试环境,生产环境.对于这些环境,可能会有不同接口调用,不同的数据库连接字符串等等.那么该如何实现不同环境的参数快速切换呢?当然,最笨的方式就是 ...
- 如何修改Mac截屏保存路径
MAC OS X系统默认的截图路径是桌面文件夹,默认的截图格式是 PNG 图片格式,如何自定义设置呢? 截图保存路径 打开终端(Terminal)并输入如下命令: defaults write com ...
- Linux学习9-CentOS搭建nginx环境
前言 之前我们搭建网站的时候,把war包放到tomcat下就能运行起来了,为什么部署上线的时候,又用到了nginx呢? nginx可以做多台服务器的负载均衡,当用户非常少的时候,可以用一台服务直接部署 ...
- Windows 下安装NPM
第一步: 下载node.js的windows版 当前最新版本是https://nodejs.org/dist/ 第二步:设置环境变量 把node.exe所在目录加入到PATH环境变量中. 配置成功 ...