当前位置:网站首页>剑指 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
边栏推荐
- PostgreSQL 存储结构浅析
- 虚拟串口模拟器和串口调试助手使用教程「建议收藏」
- Rhcsa Road
- 红队第8篇:盲猜包体对上传漏洞的艰难利用过程
- The sharp drop in electricity consumption in Guangdong shows that the substitution of high-tech industries for high-energy consumption industries has achieved preliminary results
- [daily question] 1175 Prime permutation
- She is the "HR of others" | ones character
- OJ questions related to complexity (leetcode, C language, complexity, vanishing numbers, rotating array)
- How to restore the system of Sony laptop
- 复杂度相关OJ题(LeetCode、C语言、复杂度、消失的数字、旋转数组)
猜你喜欢
In the past six months, it has been invested by five "giants", and this intelligent driving "dark horse" is sought after by capital
嗨 FUN 一夏,与 StarRocks 一起玩转 SQL Planner!
VMware 虛擬機啟動時出現故障:VMware Workstation 與 Hyper-v 不兼容...
IM即時通訊開發實現心跳保活遇到的問題
程序员职业生涯真的很短吗?
Tutorial on the principle and application of database system (003) -- MySQL installation and configuration: manually configure MySQL (Windows Environment)
Sweden announced its decision to exclude Huawei 5g equipment, but Huawei has successfully found a new way out
Stonedb is building blocks for domestic databases, and the integrated real-time HTAP database based on MySQL is officially open source!
独家消息:阿里云悄然推出RPA云电脑,已与多家RPA厂商开放合作
Défaillance lors du démarrage de la machine virtuelle VMware: le poste de travail VMware n'est pas compatible avec hyper - V...
随机推荐
用手机在同花顺上开户靠谱吗?这样有没有什么安全隐患
How to restore the system of Sony laptop
Tutorial on principles and applications of database system (006) -- compiling and installing MySQL 5.7 (Linux Environment)
Défaillance lors du démarrage de la machine virtuelle VMware: le poste de travail VMware n'est pas compatible avec hyper - V...
StoneDB 为国产数据库添砖加瓦,基于 MySQL 的一体化实时 HTAP 数据库正式开源!
Stegano in the world of attack and defense
数据库系统原理与应用教程(001)—— MySQL 安装与配置:MySQL 软件的安装(windows 环境)
怎么用MySQL语言进行行列装置?
数据库系统原理与应用教程(003)—— MySQL 安装与配置:手工配置 MySQL(windows 环境)
瑞典公布决定排除华为5G设备,但是华为已成功找到新出路
In the era of super video, what kind of technology will become the base?
[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
【SQL语句】请问这边为什么select出了两个上海,查询出了不同的count我想让他变成一个上海,count只显示一个总和
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
动作捕捉系统用于苹果采摘机器人
Why is the pkg/errors tripartite library more recommended for go language error handling?
Telecommuting experience? Let's introduce ourselves ~ | community essay solicitation
Go 语言错误处理为什么更推荐使用 pkg/errors 三方库?
ssm框架原理
【每日一题】1175. 质数排列