当前位置:网站首页>leetcode:30. 串联所有单词的子串【Counter匹配 + 剪枝】
leetcode:30. 串联所有单词的子串【Counter匹配 + 剪枝】
2022-06-23 15:40:00 【白速龙王的回眸】

分析
用个Counter记录words的出现次数
然后记录总长度和每个单词的长度
开始对s的起点遍历
然后看看从s中截取的内容的Counter是否符合
一旦有某个超过就直接剪枝即可
ac code
class Solution:
def findSubstring(self, s: str, words: List[str]) -> List[int]:
# double pointers
pattern = Counter(words)
n, m, seg, nums = len(s), len(''.join(words)), len(words[0]), len(words)
ans = []
#print(pattern)
for st in range(n - m + 1):
now = s[st: st + m]
c = Counter()
flag = True
for i in range(nums):
c[now[seg*i : seg*(i + 1)]] += 1
if c[now[seg*i : seg*(i + 1)]] > pattern[now[seg*i : seg*(i + 1)]]:
flag = False
break
if not flag:
continue
#print(c)
if c == pattern:
ans.append(st)
return ans
总结
简单Counter
边栏推荐
- 企业想上MES系统?还得满足这些条件
- 中大人脸素描FERET数据库(CUFSF)
- 解决:在验证阶段,第一个batch不会报错,第二个batch报cuda超出的错误
- HBuilderX-Light 主题能不能加个批注功能?
- Tips for accelerating file transfer between windows remote desktop connections
- 15 differences between MES in process and discrete manufacturing enterprises (Part I)
- 《Apache Commons 工具类》
- openGauss数据库源码解析系列文章—— 密态等值查询技术详解(上)
- 将vscode打造无敌的IDE(14) tasks.json和launch.json配置详解,随心所欲添加自动化任务
- Apache commons tool class
猜你喜欢

Summarize the experience of purchasing Alibaba cloud servers

golang数据类型图

安全舒适,全新一代奇骏用心诠释老父亲的爱

get_edges

Block, non block, multiplexing, synchronous, asynchronous, bio, NiO, AIO
![生成二叉搜索平衡树[利用树递归特性]](/img/b3/f8edf45bdfdced7c3698088dbf7d84.png)
生成二叉搜索平衡树[利用树递归特性]

PageHelper在面对复杂service数据处理下的分页问题
![[tcapulusdb knowledge base] Introduction to tmonitor background one click installation (I)](/img/d7/3a514fb75b3df487914a8db245ab89.png)
[tcapulusdb knowledge base] Introduction to tmonitor background one click installation (I)
![[tcapulusdb knowledge base] Introduction to tmonitor stand-alone installation guidelines (II)](/img/6d/8b1ac734cd95fb29e576aa3eee1b33.png)
[tcapulusdb knowledge base] Introduction to tmonitor stand-alone installation guidelines (II)

解决:在验证阶段,第一个batch不会报错,第二个batch报cuda超出的错误
随机推荐
Sleuth + Zipkin
Avoid these six difficulties and successfully implement MES system
PageHelper在面对复杂service数据处理下的分页问题
Web容器是怎样给第三方插件做初始化工作的
数组自带的方法
pytorch:模型的保存与导出
[tcapulusdb knowledge base] Introduction to tmonitor stand-alone installation guidelines (I)
Golang对JSON文件的写操作
Different implementation of CAS operation under arm and x86
港股多支个股表现活跃,引发投资者对港股市场回暖猜想与关注
Quartz
uniapp对接腾讯即时通讯TIM 发图片消息问题
医学影像分割的网站
CA认证和颁发吊销证书
window远程桌面连接互传文件加速小技巧
Block, non block, multiplexing, synchronous, asynchronous, bio, NiO, AIO
Improving efficiency or increasing costs, how should developers understand pair programming?
15 differences between MES in process and discrete manufacturing enterprises (Part I)
苹果 iPhone、三星手机等电子产品开始经平行进口渠道进入俄罗斯
R语言使用colorblinr包模拟色盲视觉、将已有的ggplot2可视化结果图像使用edit_colors函数编辑转化为色盲视觉友好的可视化结果、并自定设置色盲形式、色盲严重级别