当前位置:网站首页>剑指 Offer II 014. 字符串中的变位词 滑动窗口
剑指 Offer II 014. 字符串中的变位词 滑动窗口
2022-06-25 16:35:00 【Python ml】
剑指 Offer II 014. 字符串中的变位词
滑动窗口
class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
m,n=len(s1),len(s2)
if m>n:
return False
cnt_s1=Counter(s1)
for i in range(n-m+1): # i为0-n-m
if Counter(s2[i:i+m])==cnt_s1: #不包括索引i+m
return True
return False
双指针,过程中保证dict[left]至dict[right]中每个值都小于0,说明没有多余字母或其他字母,且[left,right]的长度刚好为m,说明每个字母个数都相同,不然在++dict[x]后必定存在dict[x]>0
class Solution {
public:
bool checkInclusion(string s1, string s2) {
int m=s1.length(),n=s2.length();
vector<int>dict(26);
for(int i=0;i<m;++i){
--dict[s1[i]-'a'];
}
int left=0;
for(int right=0;right<n;++right){
int x=s2[right]-'a';
++dict[x];
while (dict[x]>0){
--dict[s2[left]-'a'];
++left;
}
if(right-left+1==m) return true;
}
return false;
}
};
边栏推荐
- About: encryption and decryption of rsa+aes data transmission [chapter], project practice (special summary)
- Day_ 05
- 【无标题】
- 从业一年,我是如何涨薪13K+?
- Xinlou: un voyage de sept ans de Huawei Sports Health
- 2022-06-17 advanced network engineering (x) is-is-general header, establishment of adjacency relationship, IIH message, DIS and pseudo node
- Perfect shuffle problem
- Record learning of hystrix knowledge --20210929
- Redis系列——概述day1-1
- Bypass technology to talk about 'cross end'
猜你喜欢

論文筆記:LBCF: A Large-Scale Budget-Constrained Causal Forest Algorithm

完美洗牌问题

心樓:華為運動健康的七年築造之旅

知道这些面试技巧,让你的测试求职少走弯路

Read mysql45 - a simple understanding of global locks and table locks

【效率】又一款笔记神器开源了!

Day_ fifteen

Read mysql45 the next day

Day_ eleven

Notes: lbcf: a Large Scale budget Constrained causal Forest Algorithm
随机推荐
使用PyWebIO测试,刚入门的测试员也能做出自己的测试工具
【效率】又一款笔记神器开源了!
Redis系列——概述day1-1
WPF开发随笔收录-心电图曲线绘制
Common APIs and exception mechanisms
[Jianzhi offer II 091. painting the house]
Paper notes: lbcf: a large scale budget constrained causal forest algorithm
Day_ fifteen
根据先序遍历和中序遍历生成后序遍历
3年,我是如何涨薪到20k?
Reading mysql45 lecture - index continued
【NLP】今年英语高考,CMU用重构预训练交出134高分,大幅超越GPT3
Xinlou: Huawei's seven-year building journey of sports health
Data type variable operator
What is backbone network
居家办公让我绩效拿了C | 社区征文
Day_ ten
Effects and laws
加密潮流:时尚向元宇宙的进阶
【机器学习】基于多元时间序列对高考预测分析案例