当前位置:网站首页>剑指 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
边栏推荐
- [jetsonnano] [tutorial] [introductory series] [III] build tensorflow environment
- 【Hot100】19. 删除链表的倒数第 N 个结点
- Principes et applications du système de base de données (006) - - compilation et installation de MySQL 5.7 (environnement Linux)
- 圈铁发音,动感与无噪强强出彩,魔浪HIFIair蓝牙耳机测评
- China's intelligent transportation construction from the perspective of "one hour life circle" in Dawan District
- Tutorial on the principle and application of database system (005) -- Yum offline installation of MySQL 5.7 (Linux Environment)
- 数据库系统原理与应用教程(006)—— 编译安装 MySQL5.7(Linux 环境)
- Principle of motion capture system
- Advantages, values and risks of chain games compared with traditional games
- How long will it take to achieve digital immortality? Metacosmic holographic human avatar 8i
猜你喜欢

Sweden announced its decision to exclude Huawei 5g equipment, but Huawei has successfully found a new way out

怎麼用MySQL語言進行行列裝置?

Problems encountered in IM instant messaging development to maintain heartbeat

高端程序员上班摸鱼指南

IM即时通讯开发万人群聊消息投递方案

Kali install Nessus

Learn selenium to simulate mouse operation, and you can be lazy a little bit

Basic use of MySQL

毕业季 | 华为专家亲授面试秘诀:如何拿到大厂高薪offer?

【Hot100】19. Delete the penultimate node of the linked list
随机推荐
vim用户自动命令示例
程序员职业生涯真的很短吗?
博睿数据一体化智能可观测平台入选中国信通院2022年“云原生产品名录”
嗨 FUN 一夏,与 StarRocks 一起玩转 SQL Planner!
毕业季 | 华为专家亲授面试秘诀:如何拿到大厂高薪offer?
Mlperf training v2.0 list released, with the same GPU configuration, the performance of Baidu PaddlePaddle ranks first in the world
数据库系统原理与应用教程(005)—— yum 离线安装 MySQL5.7(Linux 环境)
运动捕捉系统原理
How to solve the keyboard key failure of notebook computer
Example of vim user automatic command
[observation] where is the consulting going in the digital age? Thoughts and actions of softcom consulting
Rhcsa Road
She is the "HR of others" | ones character
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
China's intelligent transportation construction from the perspective of "one hour life circle" in Dawan District
Is it reliable to open an account on flush with mobile phones? Is there any potential safety hazard
实现数字永生还有多久?元宇宙全息真人分身#8i
韩国AI团队抄袭震动学界!1个导师带51个学生,还是抄袭惯犯
How to use phpipam to manage IP addresses and subnets
【Hot100】19. 删除链表的倒数第 N 个结点