[LeetCode] 438. Find All Anagrams in a String_Easy
438. Find All Anagrams in a String
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s.
Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100.
The order of output does not matter.
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".
这个题目我最开始的思路是d2 = collections.Counter(p), 然后d1 = collections.Counter(s[:len(p)]), 然后每次往后移动的时候, 就改变相应的d1, 然后比较, 但是发现用Counter比较的时候有个问题, 因为d1 有可能有'c':0 的情况, 虽然跟d2是一样, 但是并不能相等. 所以用[0]*26来代替Counter, 只是比较的时候要用ord(c) - ord('a')去得到index
Code
class Solution:
def findAnagrams(self, s, p):
l_s, l_p, ans = len(s), len(p), []
if l_p > l_s: return ans
d1, d2 = [0]*26, [0]*26 # 只有小写
for i in range(l_p):
d1[ord(s[i]) - ord('a')] += 1
d2[ord(p[i]) - ord('a')] += 1
if d1 == d2: ans.append(0)
for i in range(1, l_s-l_p + 1):
d1[ord(s[i-1]) - ord('a')] -= 1
d1[ord(s[i+ l_p -1]) - ord('a')] += 1
if d1 == d2:
ans.append(i)
return ans
[LeetCode] 438. Find All Anagrams in a String_Easy的更多相关文章
- [LeetCode] 438. Find All Anagrams in a String 找出字符串中所有的变位词
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings ...
- LeetCode 438. Find All Anagrams in a String (在字符串中找到所有的变位词)
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings ...
- [leetcode]438. Find All Anagrams in a String找出所有变位词
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings ...
- 438. Find All Anagrams in a String - LeetCode
Question 438. Find All Anagrams in a String Solution 题目大意:给两个字符串,s和p,求p在s中出现的位置,p串中的字符无序,ab=ba 思路:起初 ...
- 【leetcode】438. Find All Anagrams in a String
problem 438. Find All Anagrams in a String solution1: class Solution { public: vector<int> fin ...
- 【leetcode】Find All Anagrams in a String
[leetcode]438. Find All Anagrams in a String Given a string s and a non-empty string p, find all the ...
- 438. Find All Anagrams in a String
原题: 438. Find All Anagrams in a String 解题: 两个步骤 1)就是从s中逐步截取p长度的字符串 2)将截取出的字符串和p进行比较,比较可以用排序,或者字典比较(这 ...
- 【LeetCode】438. Find All Anagrams in a String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 双指针 日期 题目地址:https://l ...
- 【leetcode❤python】 438. Find All Anagrams in a String
class Solution(object): def findAnagrams(self, s, p): """ :type s: s ...
随机推荐
- 为什么WAN口IP和外网IP不一样(不一致)?
正常的网络应该是动态公网ip,也就是路由器里面的WAN口IP与www.ip138.com上面显示的是一致的,不一致的话则说明该网络被电信或者联通做了NAT转发,导致您获取到了一个虚假的IP地址,无法用 ...
- 【Spring源码深度解析学习系列】注册解析的BeanDefinition(五)
对于配置文件,解析和装饰完成之后,对于得到的beanDefinition已经可以满足后续的使用要求了,还剩下注册,也就是processBeanDefinition函数中的BeanDefinitionR ...
- Docker监控:google/cadvisor
Docker自带了容器监控功能,可以对容器进行相关的性能监控,指标查看 主要包括: 主机的CPU情况和使用量 主机的内存情况和使用量 主机的本地镜像情况 主机的容器运行情况 常规使用docker ps ...
- Unity3D NGUI 二 NGUI Button怎样接受用户点击并调用函数,具体方法名称是什么
a.直接监听事件 把下面脚本直接绑定在按钮上,当按钮点击时就可以监听到,这种方法不太好很不灵活. void OnClick(){ Debug.Log("Button is Click!!!& ...
- [转]Android Path里FillType功能
对于简单的封闭图形(路径无相交的现象),图形的外部和内部和容易判断,但如果路径有相交的情况,对应重叠的部分,使用不同的填充模式,内部和外部的定义有所不同.填充模式可以分为两类: EVEN_ODD 意味 ...
- 9.10Django模板
2018-9-10 16:37:29 模板就一个 不能嵌套 模板: http://www.cnblogs.com/liwenzhou/p/7931828.html 2018-9-10 21:23:3 ...
- 170817、Nginx详细配置
Nginx能做什么 nginx主要是做转发,当然也可以做静态资源文件缓存,做转发的时候,比如你有几个url,可以统一通过走nginx,然后通过nginx转发到不同的url上 1.反向代理 反向代理应该 ...
- hihocoder 1829 - 压缩字符串 - [状压+暴力枚举][2018ICPC北京网络预赛B题]
题目链接:https://hihocoder.com/problemset/problem/1829 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Lara Croft, ...
- 0003python中的可变参数
>>>def foo(x,y,z,*args,**kargs): print x print y print z print args print kargs >>> ...
- CCCC L2-018. 多项式A除以B 直接上map,然后stack处理输出
https://www.patest.cn/contests/gplt/L2-018 题意:模拟多项式除法. 题解:短除法,初中奥数老师,高中数学老师,高数老师都讲过2333. 模拟之前,关于保存 多 ...