当前位置:网站首页>剑指 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
边栏推荐
- When ABAP screen switching, refresh the previous screen
- 用手机在同花顺上开户靠谱吗?这样有没有什么安全隐患
- 瑞典公布决定排除华为5G设备,但是华为已成功找到新出路
- China's intelligent transportation construction from the perspective of "one hour life circle" in Dawan District
- What is the digital transformation of manufacturing industry
- 高端程序员上班摸鱼指南
- Tutorial on principles and applications of database system (006) -- compiling and installing MySQL 5.7 (Linux Environment)
- FPN network details
- 接口测试框架中的鉴权处理
- Im instant messaging develops a message delivery scheme for 10000 people
猜你喜欢
C#/VB. Net merge PDF document
【Hot100】20. 有效的括号
Use Tencent cloud to build a map bed service
Endeavouros mobile hard disk installation
嗨 FUN 一夏,与 StarRocks 一起玩转 SQL Planner!
How to restore the system of Sony laptop
Stegano in the world of attack and defense
IM即時通訊開發實現心跳保活遇到的問題
She is the "HR of others" | ones character
Mlperf training v2.0 list released, with the same GPU configuration, the performance of Baidu PaddlePaddle ranks first in the world
随机推荐
Ring iron pronunciation, dynamic and noiseless, strong and brilliant, magic wave hifiair Bluetooth headset evaluation
Submission lottery - light application server essay solicitation activity (may) award announcement
Rhcsa Road
UML旅游管理系统「建议收藏」
P2893 [USACO08FEB] Making the Grade G(dp&优先队列)
Zabbix2.2 monitoring system and application log monitoring alarm
[JetsonNano] [教程] [入门系列] [三] 搭建TensorFlow环境
How to solve the problem that the battery icon of notebook computer does not display
【Hot100】19. 删除链表的倒数第 N 个结点
vim用户自动命令示例
Installation and use of sqoop
德国iF多项大奖加冕,这副耳机有多强?音珀GTW 270 Hybrid深度评测
Comprehensively view the value of enterprise digital transformation
Rhcsa Road
投稿开奖丨轻量应用服务器征文活动(5月)奖励公布
Microservice tracking SQL (support Gorm query tracking under isto control)
Go language source level debugger delve
SQLServer查询: a.id与b.id相同时,a.id对应的a.p在b.id对应的b.p里找不到的话,就显示出这个a.id和a.p
How to use phpipam to manage IP addresses and subnets
Telecommuting experience? Let's introduce ourselves ~ | community essay solicitation