当前位置:网站首页>剑指 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;
}
};
边栏推荐
猜你喜欢
随机推荐
代码注释的艺术,优秀代码真的不需要注释吗?
_ 19_ IO stream summary
从业一年,我是如何涨薪13K+?
AD域登录验证
Cache architecture scheme of ten million level shopping cart system
Preliminary understanding of JVM
Using pywebio testing, novice testers can also make their own testing tools
Ncnn source code learning collection
2022-06-17 advanced network engineering (IX) is-is- principle, NSAP, net, area division, network type, and overhead value
Kalman Filter 遇到 Deep Learning : 卡尔曼滤波和深度学习有关的论文
[Jianzhi offer II 091. painting the house]
Batch --07--- breakpoint lifting
剑指 Offer 39. 数组中出现次数超过一半的数字
How to view the change trend of cloud database from the behind of the launch of tidb to Alibaba cloud
Kettle表输入组件精度丢失的问题
Apijson simple to use
软件测试面试如何正确谈薪
APIJSON简单使用
The first day of reading mysql45
Paper notes: lbcf: a large scale budget constrained causal forest algorithm








