当前位置:网站首页>剑指 Offer II 015. 字符串中的所有变位词
剑指 Offer II 015. 字符串中的所有变位词
2022-07-01 16:23:00 【Python ml】
class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
s_len,p_len=len(s),len(p)
if s_len<p_len:
return []
ans=[]
count=[0]*26
for i in range(p_len):
count[ord(p[i])-ord('a')]-=1
count[ord(s[i])-ord('a')]+=1
differ=0
for c in count:
if c!=0:
differ+=1
if differ==0:
ans.append(0)
for i in range(s_len-p_len):
left=ord(s[i])-ord('a') #滑动窗口左边丢弃的字母
right=ord(s[i+p_len])-ord('a') #滑动窗口右边加入的字母
if count[left]==1: #本来字母left的数量多一个,丢弃后differ--
differ-=1
elif count[left]==0:
differ+=1
count[left]-=1
if count[right]==0:
differ+=1
elif count[right]==-1:
differ-=1
count[right]+=1
if differ==0:
ans.append(i+1)
return ans
边栏推荐
- Use Tencent cloud to build a map bed service
- Red team Chapter 10: ColdFusion the difficult process of deserializing WAF to exp to get the target
- The supply of chips has turned to excess, and the daily output of Chinese chips has increased to 1billion, which will make it more difficult for foreign chips
- Learn selenium to simulate mouse operation, and you can be lazy a little bit
- 毕业后5年,我成为了年薪30w+的测试开发工程师
- 接口测试框架中的鉴权处理
- [daily question] 1175 Prime permutation
- P2893 [USACO08FEB] Making the Grade G(dp&优先队列)
- Authentication processing in interface testing framework
- [SQL statement] Why do you select two Shanghai and query different counts here? I want it to become a Shanghai, and count only displays a sum
猜你喜欢

Sqlserver query: when a.id is the same as b.id, and the A.P corresponding to a.id cannot be found in the B.P corresponding to b.id, the a.id and A.P will be displayed

Is the programmer's career really short?

今天14:00 | 港大、北航、耶鲁、清华、加大等15位ICLR一作讲者精彩继续!

C#/VB. Net merge PDF document

高端程序员上班摸鱼指南

StoneDB 为国产数据库添砖加瓦,基于 MySQL 的一体化实时 HTAP 数据库正式开源!

嗨 FUN 一夏,与 StarRocks 一起玩转 SQL Planner!

普通二本,去过阿里外包,到现在年薪40W+的高级测试工程师,我的两年转行心酸经历...

数据库系统原理与应用教程(002)—— MySQL 安装与配置:MySQL 软件的卸载(windows 环境)

IM即时通讯开发万人群聊消息投递方案
随机推荐
How to restore the system of Sony laptop
制造业数字化转型究竟是什么
全面看待企业数字化转型的价值
韩国AI团队抄袭震动学界!1个导师带51个学生,还是抄袭惯犯
【Hot100】17. 电话号码的字母组合
Basic use of MySQL
Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
Buuctf gold III
红队第8篇:盲猜包体对上传漏洞的艰难利用过程
C#/VB. Net merge PDF document
为国产数据库添砖加瓦,StoneDB 一体化实时 HTAP 数据库正式开源!
数据库系统原理与应用教程(002)—— MySQL 安装与配置:MySQL 软件的卸载(windows 环境)
Go 语言源码级调试器 Delve
Rhcsa Road
Red team Chapter 8: blind guess the difficult utilization process of the package to upload vulnerabilities
I'm a senior test engineer who has been outsourced by Alibaba and now has an annual salary of 40w+. My two-year career changing experience is sad
Action after deleting laravel's model
数据库系统原理与应用教程(006)—— 编译安装 MySQL5.7(Linux 环境)
Origin2018 installation and use (sorting)
[nodemon] app crashed - waiting for file changes before starting...解决方法