[LeetCode]题解(python):030-Substring with Concatenation of All Words
题目来源
https://leetcode.com/problems/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 starting indices of substring(s) in s that is a concatenation of each word in wordsexactly once and without any intervening characters.
题意分析
Input:a str and a list
Output:a list recorded the indices
Conditions:输入一个字符串s和一连串的长度相同的字符串数组words,找出仅由所有的words组成的s的子字符串起始位置。words内的元素可能重复,但是每个元素都需要出现(如有两个‘good’元素,那么’good‘就需要出现两次)
examples:
s: "barfoothefoobarman"
words: ["foo", "bar"]
You should return the indices: [0,9].
题目思路
- 首先将words存为一个dic(dictiionary(字典))(出现一次value为1,出现两次value为2,以此类推)
- 遍历每一个可能的str(长度等于len(words)*len(words[0]))
- 对每一个str,用一个新的字典d来存储已经出现的word,用python的分片来访问每一个str里面的word,用计数器count计数相等的word
- 条件检验:如果元素不在dic,直接break取下一个可能的str;如果元素在dic,但是不在d,直接将新元素加入到d,value取为1,count加1;如果元素在dic,同时也在d,看此时d中的value是否小于dic中的value,小于则value加1,count加1,否则此字串不满足条件,舍弃取下一条str
- 每一次对一个str处理后,d要清空,count要置为0
- 注意事项:d.get(each_str, -1) != -1的速度不如 each_str in d
AC代码(Python)
_author_ = "YE"
# -*- coding:utf-8 -*-
# SH……把d.get(each_str, -1) == -1类似的判断改为 each_str not in d 就AC了……说明后者效率高呀
def findSubstring(s, words):
"""
:type s: str
:type words: List[str]
:rtype: List[int]
"""
import math
rtype = []
dic = {} len_word = len(words)
if len_word == 0:
return rtype
#print('len of words:%s' % len_word) len_each_word = len(words[0])
#print('len of each word:%s' % len_each_word) all_len = len_word * len_each_word
#print("all len:", all_len) for x in words:
if x in dic:
dic[x] = dic[x] + 1
else:
dic[x] = 1 #print('dictionary:')
#print(dic) #print(dic.get('arf',-1) == -1) len_s = len(s)
#print('the len of the str s', len_s) if len_s < all_len:
return rtype #print('From loop') for i in range(len_s - all_len + 1):
str = s[i:i+all_len]
count = 0
d = {}
for j in range(len_word):
each_str = str[j * len_each_word:(j + 1) * len_each_word]
if each_str not in dic:
count = 0
break
elif each_str in d:
if d[each_str] == dic[each_str]:
count = 0
break
else:
d[each_str] = d[each_str] + 1
count = count + 1
continue
else:
d[each_str] = 1
count = count + 1
if count == len_word:
rtype.append(i)
count = 0
#print(rtype)
[LeetCode]题解(python):030-Substring with Concatenation of All Words的更多相关文章
- leetcode python 030 Substring with Concatenation of All Words
## 您将获得一个字符串s,以及一个长度相同单词的列表.## 找到s中substring(s)的所有起始索引,它们只包含所有单词,## eg:s: "barfoothefoobarman&q ...
- [Leetcode][Python]30: Substring with Concatenation of All Words
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 30: Substring with Concatenation of All ...
- LeetCode 030 Substring with Concatenation of All Words
题目要求:Substring with Concatenation of All Words You are given a string, S, and a list of words, L, th ...
- Java for LeetCode 030 Substring with Concatenation of All Words【HARD】
You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...
- leetcode第29题--Substring with Concatenation of All Words
problem: You are given a string, S, and a list of words, L, that are all of the same length. Find al ...
- 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 star ...
- 030 Substring with Concatenation of All Words 与所有单词相关联的字串
给定一个字符串 s 和一些长度相同的单词 words,找出 s 与 words 中所有单词(words 每个单词只出现一次)串联一起(words中组成串联串的单词的顺序随意)的字符串匹配的所有起始索引 ...
- 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 ...
- leetcode题解 3. Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- 《LeetBook》leetcode题解(3):Longest Substring Without Repeating Characters[M]——哈希判断重复
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
随机推荐
- kmp 和boyer-moore
<html> <head> <meta http-equiv="content-type" content="text/html; char ...
- HDU 4003 (树形DP+背包)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4003 题目大意:有K个机器人,走完树上的全部路径,每条路径有个消费.对于一个点,机器人可以出去再回来 ...
- Eclipse远程调试(远程服务器端监听)
前提:远程服务器上运行的WEB项目class对应的源码与本地项目中必须保持一致,也就是远程tomcat部署的项目就是本机项目打包过去的,而本机项目没有发生变动. 远程服务器端 服务器端配置eclips ...
- FXK Javascript
Javascript是一门神奇的语言,很不爽的一门语言,很纠结的一门语言. 以下内容,专业人士请不要看,只供像我一样的菜鸟参考. (1)Javascript找不到函数.明明已经引用了JS文件,却提示找 ...
- 在Excel中实现查询功能
$sn = Read-Host -Prompt "请输入员工号|序列号|资产号" $xl = New-Object -ComObject "Excel.Applicati ...
- CSS系列:表达式(Expression)`淘汰`
概述 CSS表达式是动态设置CSS属性的强大(但危险)方法.Internet Explorer从第5个版本开始支持CSS表达式. 兼容性 expression方法在其它浏览器中不起作用,因此在跨浏览器 ...
- Apache Spark源码走读之17 -- 如何进行代码跟读
欢迎转载,转载请注明出处,徽沪一郎 概要 今天不谈Spark中什么复杂的技术实现,只稍为聊聊如何进行代码跟读.众所周知,Spark使用scala进行开发,由于scala有众多的语法糖,很多时候代码跟着 ...
- 在archlinux上搭建twitter storm cluster
本文详细描述如何在archlinux上搭建twitter storm cluster,转载请注明出处,谢谢. 有关archlinux基本系统安装,请参照archlinux简明安装指南一文,下面以上述为 ...
- 页面静态化3 --- 使用PHP页面缓存机制来完成页面静态化(下)操作一个案例(新闻管理系统)
案例需求: 使用PHP缓存机制完成新闻管理系统的页面静态化数据库表 ecs_article (新闻表)因为新闻这些信息,并不是对实时性要求高,本身这个新闻比较稳定,内容也比较固定,所以我们考虑: 当第 ...
- data abstractions 数据抽象
Computer Science An Overview _J. Glenn Brookshear _11th Edition In this chapter we investigate how d ...