当前位置:网站首页>438. Find All Anagrams in a String
438. Find All Anagrams in a String
2022-06-23 08:46:00 【ujn20161222】
438. Find All Anagrams in a String
Medium
7148250Add to ListShare
Given two strings s and p, return an array of all the start indices of p's anagrams in s. You may return the answer in any order.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example 1:
Input: s = "cbaebabacd", p = "abc" Output: [0,6] Explanation: The substring with start index = 0 is "cba", which is an anagram of "abc". The substring with start index = 6 is "bac", which is an anagram of "abc".
Example 2:
Input: s = "abab", p = "ab" Output: [0,1,2] Explanation: The substring with start index = 0 is "ab", which is an anagram of "ab". The substring with start index = 1 is "ba", which is an anagram of "ab". The substring with start index = 2 is "ab", which is an anagram of "ab".
Constraints:
1 <= s.length, p.length <= 3 * 104sandpconsist of lowercase English letters.
Accepted
507,570
Submissions
1,056,733
class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
dict_p,dict_s=Counter(p),defaultdict(int)
num_to_correct,ans=len(dict_p),[]
for i in range(len(s)):
new_s=s[i]
dict_s[new_s]+=1
if dict_s[new_s]==dict_p[new_s]:
num_to_correct-=1
if dict_s[new_s]==dict_p[new_s]+1:
num_to_correct+=1
if i >= len(p):
old_s=s[i-len(p)]
dict_s[old_s]-=1
if dict_s[old_s]==dict_p[old_s]-1:
num_to_correct+=1
if dict_s[old_s]==dict_p[old_s]:
num_to_correct-=1
if num_to_correct==0: ans.append(i+1-len(p))
return ansdict_s Record each char The number of
Every time the quantity changes , Maintenance
边栏推荐
- Self organizing map neural network (SOM)
- ArcLayoutView: 一个弧形布局的实现
- Third party payment in the second half: scuffle to symbiosis
- Part of speech family
- Spirit matrix for leetcode topic analysis
- [QNX Hypervisor 2.2用户手册]6.2 网络
- [cloud computing] GFS ideological advantages and architecture
- TDesign update weekly report (the first week of January 2022)
- How thingjs enables low threshold 3D visualization development
- Le rapport d'analyse de l'industrie chinoise des bases de données a été publié en juin. Le vent intelligent se lève, les colonnes se régénèrent
猜你喜欢

3. Caller 服务调用 - dapr

Hongmeng reads the resource file

Open source technology exchange batch stream integrated data synchronization engine Chunjun data restore DDL function module analysis

最常用的5中流ETL模式

Testing -- automated testing selenium (about API)

Basic use of check boxes and implementation of select all and invert selection functions

Keng dad's "dedication blessing": red packet technology explosion in Alipay Spring Festival Gala

Linux Mysql安装

测试-- 自动化测试selenium(关于API)

复选框的基本使用与实现全选和反选功能
随机推荐
Top 25 most popular articles on vivo Internet technology in 2021
单编内核驱动模块
Lightweight UI control library worth collecting
How to use the template library of barcode label software
Which one is better for rendering renderings? 2022 latest measured data (IV)
【云计算】GFS思想优势以及架构
[cloud computing] GFS ideological advantages and architecture
2-用线段构成图形、坐标转换
You have a string of code, but do not support the lower version of go; Judge the go version number, you deserve it!
Focus! Ten minutes to master Newton convex optimization
1、 Software architecture evaluation
训练后的随机森林模型导出和加载
史上最污技术解读,60 个 IT 术语我居然秒懂了......
Restore the default routing settings of the primary network card
528. Random Pick with Weight
复选框的基本使用与实现全选和反选功能
'教练,我想打篮球!' —— 给做系统的同学们准备的 AI 学习系列小册
Arclayoutview: implementation of an arc layout
5、 Project management
3-ProgressBar和二次裁剪