当前位置:网站首页>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
边栏推荐
- 高通9x07两种启动模式
- Leetcode topic analysis h-index II
- Go 数据类型篇(二)之Go 支持的数据类型概述及布尔类型
- C # advanced learning -- virtual method
- Analysis of JMeter pressure measurement results
- 3. caller service call - dapr
- Which one is better for rendering renderings? 2022 latest measured data (IV)
- How to solve the problem that flv video stream cannot be played and TS file generation fails due to packet loss?
- [cloud computing] GFS ideological advantages and architecture
- 6-shining laser application of calayer
猜你喜欢

坑爹的“敬业福”:支付宝春晚红包技术大爆发

173. Binary Search Tree Iterator
![[paper notes] catching both gray and black swans: open set supervised analog detection*](/img/52/787b25a9818cfc6a1897af81d41ab2.png)
[paper notes] catching both gray and black swans: open set supervised analog detection*

Talk about the implementation principle of @autowired

Point cloud library PCL from introduction to mastery Chapter 10

6月《中国数据库行业分析报告》发布!智能风起,列存更生

自组织映射神经网络(SOM)

Analysis of JMeter pressure measurement results

Linux Mysql安装

Introduction to typescript and basic types of variable definitions
随机推荐
Leetcode topic analysis group anagrams
List interface three sub implementation classes
986. Interval List Intersections
3-progressbar and secondary cropping
Linux MySQL installation
Use newbeecoder UI implements data paging
Basic use of check boxes and implementation of select all and invert selection functions
Open source stealing malware mercurial found in the field for "educational purposes"
523. Continuous Subarray Sum
Hongmeng reads the resource file
Point cloud library PCL from introduction to mastery Chapter 10
438. Find All Anagrams in a String
ArcLayoutView: 一个弧形布局的实现
There are some limitations in cluster expansion and contraction
Unique paths II of leetcode topic analysis
6月《中國數據庫行業分析報告》發布!智能風起,列存更生
Leetcode topic analysis h-index II
Generate code 39 extension code in batch through Excel file
125. Valid Palindrome
Data assets are king, analyzing the relationship between enterprise digital transformation and data asset management