当前位置:网站首页>剑指 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;
}
};
边栏推荐
- This latest research has revealed two secrets of cloudy development
- WPF开发随笔收录-心电图曲线绘制
- The problem of missing precision of kettle table input components
- tensorflow 旧版本
- MySQL_ JDBC
- Unity技术手册 - 干扰/噪音/杂波(Noise)子模块
- PLSQL 存储函数SQL编程
- Tensorflow old version
- Kalman time series prediction
- Ten thousand volumes - list of Dali wa
猜你喜欢

Day_ seventeen

六大专题全方位优化,阿里巴巴性能优化小册终开源,带你直抵性能极致

二十九-使用RealSenseD435进行ORBSLAM2实时三维重建

【无标题】

Android修行手册之Kotlin - 自定义View的几种写法

Kalman filter meets deep learning: papers on Kalman filter and deep learning

Day_ ten

Wireshark网卡无法找到或没有显示的问题

Day_ 04

Notes: lbcf: a Large Scale budget Constrained causal Forest Algorithm
随机推荐
Knowing these interview skills will help you avoid detours in your test job search
【 apprentissage automatique】 cas de prévision et d'analyse de l'examen d'entrée à l'Université basé sur des séries chronologiques multiples
Bypass technology to talk about 'cross end'
Optimization of lazyagg query rewriting in parsing data warehouse
Android修行手册之Kotlin - 自定义View的几种写法
【機器學習】基於多元時間序列對高考預測分析案例
XShell连接VMWare虚拟机
pytorch官方文档学习记录
完美洗牌问题
The art of code annotation. Does excellent code really need no annotation?
2022-06-17 网工进阶(九)IS-IS-原理、NSAP、NET、区域划分、网络类型、开销值
Kalman Filter 遇到 Deep Learning : 卡尔曼滤波和深度学习有关的论文
【无标题】
et al和etc区别
mysql使用过程中遇到的问题
从业一年,我是如何涨薪13K+?
Day21 multithreading
Collection overview, array encapsulation
vscode插件自用
Effects and laws