当前位置:网站首页>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
边栏推荐
- R语言使用yardstick包的rmse函数评估回归模型的性能、评估回归模型在每个交叉验证(或者重采样)的每一折fold上的RMSE、以及整体的均值RMSE(其他指标mae、mape等计算方式类似)
- 港股多支个股表现活跃,引发投资者对港股市场回暖猜想与关注
- JSON in MySQL_ Extract function description
- 《利用CAS实现自旋锁》
- R语言ggplot2可视化水平箱图(Horizontal boxplot with coord_flip)、并添加抖动数据点显示分布情况(jittered points)
- 子级文件拖到上一级
- Leetcode 450.删除二叉搜索树中的结点
- How to quickly respond to changing production management needs?
- Log4j log integration and configuration details
- Does the enterprise want to use the MES system? These conditions have to be met
猜你喜欢
随机推荐
Golang对JSON文件的读写操作
怎样快速的应对变动的生产管理需求?
提高效率 Or 增加成本,开发人员应如何理解结对编程?
npm 如何发包 删包
Uniapp sends picture messages to Tencent instant messaging Tim
泰山OFFICE技术讲座:使用字体斜体的四种情形
【TcaplusDB知识库】TcaplusDB Tmonitor模块架构介绍
window远程桌面连接互传文件加速小技巧
CA认证和颁发吊销证书
Can the hbuilderx light theme be annotated?
openGauss数据库源码解析系列文章—— 密态等值查询技术详解(上)
批量注册组件
npm install 问题解决(nvm安装与使用)
Use of iscellstr function in MATLAB
线程池
SaaS 云工具,产业互联网下的变革利器
How to configure PostgreSQL data source on SSRs page
[tcapulusdb knowledge base] Introduction to tmonitor background one click installation (I)
Analysis of TCP three-time handshake and four-time handshake
TCP protocol notes






