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的更多相关文章

  1. [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 ...

  2. 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 ...

  3. [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 ...

  4. 438. Find All Anagrams in a String - LeetCode

    Question 438. Find All Anagrams in a String Solution 题目大意:给两个字符串,s和p,求p在s中出现的位置,p串中的字符无序,ab=ba 思路:起初 ...

  5. 【leetcode】438. Find All Anagrams in a String

    problem 438. Find All Anagrams in a String solution1: class Solution { public: vector<int> fin ...

  6. 【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 ...

  7. 438. Find All Anagrams in a String

    原题: 438. Find All Anagrams in a String 解题: 两个步骤 1)就是从s中逐步截取p长度的字符串 2)将截取出的字符串和p进行比较,比较可以用排序,或者字典比较(这 ...

  8. 【LeetCode】438. Find All Anagrams in a String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 双指针 日期 题目地址:https://l ...

  9. 【leetcode❤python】 438. Find All Anagrams in a String

    class Solution(object):    def findAnagrams(self, s, p):        """        :type s: s ...

随机推荐

  1. java(5) 线程

    1.理清概念 并行与并发: *并行:多个cpu实例或者多台机器同时执行一段处理逻辑,是真正的同时. *并发:通过cpu调度算法,让用户看上去同时执行,实际上从cpu操作层面不是真正的同时.并发往往在场 ...

  2. Eclipse的控制台console经常闪现

    Eclipse的控制台console有时候经常闪现!  让它不经常的调出来,可以按下面的操作去掉它: windows  ->   preferences   ->  run/debug   ...

  3. wget 无法建立ssl连接 [ERROR: certificate common name ?..ssl.fastly.net?.doesn?. match requested host name ?.ache.ruby-lang.org?. To connect to cache.ruby-lang.org insecurely, use ?.-no-check-certificate?]

    通过wget下载文件,报错 [root@Redmine-186 opt]# wget https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.6.tar.g ...

  4. Unity3D笔记十九 持久化数据

    1.PlayerPrefs类(生命周期???) 1.1 保存与读取数据 在C#中类似缓存.Cookie.Session等保存数据的,但是有点区别的是在C#中如果在取值时没有取到默认值则返回值是NULL ...

  5. maven常用的plugin

    maven-compiler-plugin 编译Java源码,一般只需设置编译的jdk版本   <plugin> <groupId>org.apache.maven.plugi ...

  6. Centos 添加永久路由

    今天在用虚拟机测试的时候,突然发现外网不通了,记得之前加过路由的,重启网络服务后就没了,仔细一想,应该是添加的路由是临时的,一重启就没了,于是乎就有了想把它永久写入的冲动,在看了一些文档之后,只要在/ ...

  7. Linq 多连接及 left join 实例 记录

    var retList = from d in mbExList.Cast<MaterialBaseEx>().ToList() join c in umcList.Cast<Cla ...

  8. PAT甲1038 Recover the smallest number

    1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...

  9. PAT甲1005 Spell it right【字符串】

    1005 Spell It Right (20 分) Given a non-negative integer N, your task is to compute the sum of all th ...

  10. ArcGIS API for javascript开发笔记(三)——解决打印输出的中文为乱码问题

    感谢一路走来默默支持和陪伴的你~~~ ----------------------欢迎来访,拒绝转载---------------------- 1.      调用ArcGIS API的Print实 ...