当前位置:网站首页>Sword finger offer II 014 A sliding window of anagrams in strings
Sword finger offer II 014 A sliding window of anagrams in strings
2022-06-25 17:04:00 【Python ml】
The finger of the sword Offer II 014. An anamorphic word in a string
The sliding window
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 by 0-n-m
if Counter(s2[i:i+m])==cnt_s1: # Excluding the index i+m
return True
return False
Double pointer , In process assurance dict[left] to dict[right] Each value in is less than 0, Explain that there are no extra letters or other letters , And [left,right] The length of is just m, Explain that each letter has the same number , Otherwise, in the ++dict[x] The latter must exist 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;
}
};
边栏推荐
猜你喜欢

Pytorch official document learning record

How smart PLC constructs ALT instruction

2022-06-17 advanced network engineering (IX) is-is- principle, NSAP, net, area division, network type, and overhead value

Uncover ges super large scale graph computing engine hyg: Graph Segmentation

Vscode plug-in self use

Babbitt yuan universe daily recommendation: three players holding "tens of millions" of collections have revealed the "three routines" of the digital collection market

1-8Vmware中的文件共享

Using pywebio testing, novice testers can also make their own testing tools

论文笔记:LBCF: A Large-Scale Budget-Constrained Causal Forest Algorithm

JVM内存结构
随机推荐
Tensorflow old version
单例模式应用
Which is better for intermediate and advanced soft exam?
STM32硬件错误HardFault_Handler的处理方法
Creating a uniapp project using hbuilder x
Next.js 热更新 Markdown 文件变更
XXIX - orbslam2 real-time 3D reconstruction using realsensed435
Wireshark网卡无法找到或没有显示的问题
【 apprentissage automatique】 cas de prévision et d'analyse de l'examen d'entrée à l'Université basé sur des séries chronologiques multiples
Ten thousand volumes - list of Dali wa
PLSQL storage function SQL programming
好胖子带你学Flink系列-Flink源码剖析第一集Standalone启动脚本分析
【微服务|Sentinel】流控规则概述|针对来源|流控模式详解<直接 关联 链路>
居家办公让我绩效拿了C | 社区征文
Redis series - overview day1-1
IO stream
剑指 Offer II 012. 左右两边子数组的和相等
Paper notes: lbcf: a large scale budget constrained causal forest algorithm
TCP聊天+传输文件服务器服务器套接字v2.8 - 修复已知程序4个问题
软考中的嵌入式系统设计师为什么考的人少?