【leetcode】30. Substring with Concatenation of All Words

news/2024/7/5 5:43:16

题目如下:

 

解题思路:本题题干中有一个非常关键的前提,就是words中的所有word的长度一样,并且都要被使用到。所以,我们可以把输入的s按word的长度进行等分,以s = "barfoothefoobarman",words = ["foo","bar"]为例,下表是等分的结果。为什么只分到startInx=[0,2]? 因为word的长度就是3,startInx = 3是startInx = 0的子集。分解完成后,再对startInx=[0,2]中每一种分解的方法进行比对。例如startInx = 0的时候,因为words的长度是2,所以用word1和word2组成集合和words比对,比对完成后记录结果,同时删除word1,把word3加入集合,直到wordn加入集合为止。这样就能得到所有能满足题意的all starting indices of substring(s) in s。

代码如下:

class Solution(object):def findSubstring(self, s, words):""":type s: str:type words: List[str]:rtype: List[int]"""import bisectif len(words) == 0:return []words.sort()res = []length = len(words[0])for i in range(length):start = il = []ol = []while start + length <= len(s):l.append(s[start:start+length])bisect.insort_left(ol,s[start:start+length])start += lengthif len(l) == len(words):if ol == words:res.append(start - length*len(l))del ol[bisect.bisect_left(ol,l.pop(0))]return res

 

转载于:https://www.cnblogs.com/seyjs/p/9660599.html


http://lihuaxi.xjx100.cn/news/237047.html

相关文章

GO语言有哪些优势?怎样入门?

链客&#xff0c;专为开发者而生&#xff0c;有问必答&#xff01; 此文章来自区块链技术社区&#xff0c;未经允许拒绝转载。 1、学习曲线 它包含了类C语法、GC内置和工程工具。这一点非常重要&#xff0c;因为Go语言容易学习&#xff0c;所以一个普通的大学生花一个星期就能…

docker手册_Docker手册

docker手册The concept of containerization itself is pretty old, but the emergence of the Docker Engine in 2013 has made it much easier to containerize your applications. 容器化本身的概念还很老&#xff0c;但是Docker Engine在2013年的出现使容器化应用程序变得更…

shell编程--case判断

case基础语法&#xff1a;格式 case 变量名 invalue1)command;;value2)command;;*)commond;;esac在case程序中&#xff0c;可以在条件中使用|&#xff0c;表示或的意思&#xff0c; 比如 2|3)command;;脚本 [rootlynn-04 shell]# vim case.sh#!/bin/bash read -p "Ple…

Data Artisans发布支持ACID事务的流式处理框架Streaming Ledger

data Artisans宣布推出Streaming Ledger&#xff0c;它扩展了Apache Flink&#xff0c;提供了跨表、键和事件流执行可序列化ACID事务的功能。这项正在申请专利的技术是Flink的专有附加技术&#xff0c;超越了当前一次只能在一个键上实现一致性的标准。\\在发布Streaming Ledger…

你没听说过的Go语言惊人优点

链客&#xff0c;专为开发者而生&#xff0c;有问必答&#xff01; 此文章来自区块链技术社区&#xff0c;未经允许拒绝转载。 在这篇文章中&#xff0c;我将讨论为什么你需要尝试一下 Go 语言&#xff0c;以及应该从哪里学起。 Go 语言是可能是最近几年里你经常听人说起的编…

Excel VBA教程–如何使用Visual Basic在电子表格中编写代码

介绍 (Introduction) This is a tutorial about writing code in Excel spreadsheets using Visual Basic for Applications (VBA).这是一个有关使用Visual Basic for Applications(VBA)在Excel电子表格中编写代码的教程。 Excel is one of Microsoft’s most popular product…

VC解析XML--使用CMarkup类解析XML

经过今天尝试MFC解析XML串&#xff0c;也算有了不少收获&#xff0c;总结一下。 我是使用的CMarkup类对XML进行操作。 CMarkup好象都是先从一个xml文件里面把内容读出来&#xff0c;再进行解析&#xff0c;搞得我恨不得要把我的CString写到xml文件…

智课雅思词汇---十、pend是什么意思

智课雅思词汇---十、pend是什么意思 一、总结 一句话总结&#xff1a;【词根含义】&#xff1a;悬挂,垂;称量;支付 词根&#xff1a;-pend-, -pens- 【词根含义】&#xff1a;悬挂,垂;称量;支付 【词根来源】&#xff1a;来源于拉丁语动词pendeo, pendere, pependi, - (悬挂,下…