题目来源


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].


题目思路


  1. 首先将words存为一个dic(dictiionary(字典))(出现一次value为1,出现两次value为2,以此类推)
  2. 遍历每一个可能的str(长度等于len(words)*len(words[0]))
  3. 对每一个str,用一个新的字典d来存储已经出现的word,用python的分片来访问每一个str里面的word,用计数器count计数相等的word
  4. 条件检验:如果元素不在dic,直接break取下一个可能的str;如果元素在dic,但是不在d,直接将新元素加入到d,value取为1,count加1;如果元素在dic,同时也在d,看此时d中的value是否小于dic中的value,小于则value加1,count加1,否则此字串不满足条件,舍弃取下一条str
  5. 每一次对一个str处理后,d要清空,count要置为0
  6. 注意事项: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的更多相关文章

  1. leetcode python 030 Substring with Concatenation of All Words

    ## 您将获得一个字符串s,以及一个长度相同单词的列表.## 找到s中substring(s)的所有起始索引,它们只包含所有单词,## eg:s: "barfoothefoobarman&q ...

  2. [Leetcode][Python]30: Substring with Concatenation of All Words

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 30: Substring with Concatenation of All ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 030 Substring with Concatenation of All Words 与所有单词相关联的字串

    给定一个字符串 s 和一些长度相同的单词 words,找出 s 与 words 中所有单词(words 每个单词只出现一次)串联一起(words中组成串联串的单词的顺序随意)的字符串匹配的所有起始索引 ...

  8. 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 ...

  9. leetcode题解 3. Longest Substring Without Repeating Characters

    题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...

  10. 《LeetBook》leetcode题解(3):Longest Substring Without Repeating Characters[M]——哈希判断重复

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

随机推荐

  1. BZOJ1185 : [HNOI2007]最小矩形覆盖

    求出凸包后,矩形的一条边一定与凸包的某条边重合. 枚举每条边,求出离它最远的点和离它最左最右的点,因为那三个点是单调变化的,所以复杂度为$O(n)$. 注意精度. #include<cstdio ...

  2. MONO 使用重要提示

    重要提示:如果要使用mvc这样的框架(网址没有扩展名或扩展名没有正常规律),请一定用Jexus,而不要用apache/nginx等. ASP.NET跨平台初学者要注意: 1.不要开始就用freeBSD ...

  3. 【POJ】3255 Roadblocks(次短路+spfa)

    http://poj.org/problem?id=3255 同匈牙利游戏. 但是我发现了一个致命bug. 就是在匈牙利那篇,应该dis2单独if,而不是else if,因为dis2和dis1相对独立 ...

  4. OFFICE 修改记录保存在单元格批注中vba

    Dim ydtext As String '原单元格值 Private Sub Worksheet_Change(ByVal Target As Range) If Target.Cells.Coun ...

  5. linux源码组织

    linux源代码在https://www.kernel.org/就可以下.现在的稳定版本是3.16.3. 因为简历上有个项目是内核有关的,为了准备一下面试,还是要重温一下内核才行.最基本的,哪些文件在 ...

  6. Scrum会议3

    组名称:天天向上 项目名称:连连看 参会成员:王森(Master)张金生 张政 栾骄阳 时间:2016.10.18 已完成内容: 1.GUI布局设计 2.通过在网上大量阅览代码,大体了解连连看游戏制作 ...

  7. ASP.NET MVC系列 框架搭建(二)之仓储层的优化

    大神勿喷,小神默默学. 会了就是不值一提的东西,不会就是绝对的高大上. 最后上传源码.希望能给读者带来一些新的认识及知识. 还没上过头条..各位大神,请点支持一下小弟. 陆续更新.更新到你会为止!! ...

  8. ptmalloc2源码解析初探

    本文是徽沪一郞在学习华庭(庄明强)所撰<glibc内存管理-ptmalloc2源代码分析>的阅读笔记.本笔记以slides的方式加以呈现.文件采用latex+tikz编辑而成,如果对lat ...

  9. php中ajax跨域请求---小记

    php中ajax跨域请求---小记   前端时间,遇到的一个问题,情况大约是这样: 原来的写法: 前端js文件中: $.ajax({ type:'get', url:'http://wan.xxx.c ...

  10. 浅析php学习的路线图

    一直都想走上码农的道路,奈何当年没有学好.一直与码农无缘.现在又想做一些码农就下了一套某个培训机构的php视频来看.希望能走上码农的道路     1.php初级教程 初级教程主要的页面设置的,就是 h ...