[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 ...
随机推荐
- 数据库操作类——C#
整理数据库操作类以便取用: using System; using System.Collections.Generic; using System.Linq; using System.Web; u ...
- OpenNI2 + NiTE2开发教程
发现了一个非常不错的关于自然交互OpeNI2+NiTE2的资源,非常感谢Heresy,这里分享链接: OpenNI 2.x 教学文章(转载自:Heresy博客,地址:https://kheresy.w ...
- SPI中的极性CPOL和相位CPHA
详解SPI中的极性CPOL和相位CPHA SPI由于接口相对简单(只需要4根线),用途算是比较广泛,主要应用在 EEPROM,FLASH, 实时时钟,AD转换器,还有数字信号处理器和数字信号解码器之间 ...
- vbs学习笔记1——判断文件和文件夹是否存在
首先分享一个“VBS脚本常用经典代码收集”,这里面关于vbs很丰富的内容. 所有vbs脚本都需要保存为.vbs形式才可以运行 FileSystemObject Object的所有方法参考:http:/ ...
- Golang Vendor 包机制 及 注意事项
现在的 Go 版本是 1.8,早在 1.5 时期,就有了 Vendor 包机制,详情可查看博文:“理解 Go 1.5 vendor”. 遇到的问题 个人在使用 Glide 管理 Vendor 包时(附 ...
- LAMP学习路线图
站点开发概述 LAMP开发概述 HTML基础 CSS基础 DIV+CSS Javascript Jquery(Ajax) WAMP 环境搭建 PHP基本的语法,变量.数据类型,表达式,常量,流程控制, ...
- React和Vue特性和书写差异
Vue均使用ES6语法,主要以单文件组件为例,写法上优先使用缩写. React使用TS语法. 生命周期 Vue React 入口&根实例 Vue const app = new Vue({ / ...
- 跟踪EBS客户端的IP地址
Meterlink参考文档: How to Track IP Address of the Form Session in Oracle application 11i (文档 ID 878931.1 ...
- 高通与MTK瓜分天下?手机处理器品牌分析
http://mobile.pconline.com.cn/337/3379352.html [PConline 杂谈]如果你向朋友请教买一台怎样的台式机或者笔记本的话,很多时候那朋友会根据你对电脑的 ...
- python测试开发django-26.表单提交之post登录案例
前言 注册和登录功能实现都是post请求接口,只不过注册是往数据库插入数据,登录是从数据库里面查询数据. 本篇接着上一篇写个简单的登录页面请求,用户注册时密码加密存储,用户登录时候对输入的密码校验. ...