当前位置:网站首页>剑指 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;
}
};
边栏推荐
- Perfect shuffle problem
- 八种button的hover效果
- Do you know all the configurations of pychrm?
- 【機器學習】基於多元時間序列對高考預測分析案例
- Mac PHP multi version management and swoole extension installation
- Read mysql45 lecture - index
- 2022-06-17 网工进阶(九)IS-IS-原理、NSAP、NET、区域划分、网络类型、开销值
- Ad domain login authentication
- Ten thousand volumes - list of Dali wa
- How did I raise my salary to 20k in three years?
猜你喜欢

Day_ ten

Ten thousand volumes - list of Dali wa

Mac PHP multi version management and swoole extension installation

Batch --07--- breakpoint lifting

Problems encountered in using MySQL

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

Day_ seventeen

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

揭秘GES超大规模图计算引擎HyG:图切分

3.条件概率与独立性
随机推荐
2022-06-17 网工进阶(九)IS-IS-原理、NSAP、NET、区域划分、网络类型、开销值
Knowing these interview skills will help you avoid detours in your test job search
Final, override, polymorphic, abstract, interface
_ 17 collection overview
The first day of reading mysql45
A TDD example
Effects and laws
知道这些面试技巧,让你的测试求职少走弯路
The third day of mysql45
Day21 multithreading
Problems encountered in using MySQL
First knowledge of database
Optimization of lazyagg query rewriting in parsing data warehouse
Do you know all the configurations of pychrm?
解析数仓lazyagg查询重写优化
pytorch官方文档学习记录
万卷书 - 大力娃的书单
揭秘GES超大规模图计算引擎HyG:图切分
vscode插件自用
Paper notes: lbcf: a large scale budget constrained causal forest algorithm