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. 微信内置浏览器浏览H5页面弹出的键盘遮盖文本框的解决办法(转)

    最近在做微信公众号的内嵌页面,发现点击输入框时键盘盖住文本框,找到一段代码解决了这个问题. iOS和android手机都已亲测,需要的可以直接拷贝到代码中使用. js代码如下: $(function ...

  2. EXCEL通俗易懂讲公式(一):sumif,sumifs,countif,countifs

    最近公司招了一批新人,excel基本都是小白阶段,以前用过的也就是画个课程表,没做过什么数据统计和文本计算等工作.因此各种问题都来了,什么vlookup,offset,连条件求和的sumif也不会用, ...

  3. 如何验证 Email 地址:SMTP 协议入门教程

    http://www.ruanyifeng.com/blog/2017/06/smtp-protocol.html 作者: 阮一峰 日期: 2017年6月25日   Email 是最常用的用户识别手段 ...

  4. Android Usb Camera HAL框架

  5. Xcode脚本自动化打包问题:xcrun: error: unable to find utility "PackageApplication", not a developer tool or in PATH

    运行脚本后报错: xcrun: error: unable to find utility "PackageApplication", not a developer tool o ...

  6. linux中守护进程启停工具start-stop-daemon

    1.功能作用 启动和停止系统守护程序 2.位置 /sbin/start-stop-daemon 3.主要参数 Commands: -S|--start -- <argument> ... ...

  7. 微信小游戏 修改appid

    微信开发者工具中,当你使用一个公众号开发一个项目,有需求切换到另外一个公众号继续开发时,需要修改appid. 修改微信小游戏 project.config.json 文件的appid

  8. C# 队列(Queue)解决简单并发

    日志例子: private static Queue<string> m_Message = new Queue<string>(); private static bool ...

  9. 【CF878C】Tournament set+并查集+链表

    [CF878C]Tournament 题意:有k个项目,n个运动员,第i个运动员的第j个项目的能力值为aij.一场比赛可以通过如下方式进行: 每次选出2个人和一个项目,该项目能力值高者获胜,败者被淘汰 ...

  10. 9.7 Django 书单列表页面

    昨天的迭代版本,增加了编辑出版社,编辑列表,增添了返回页面! 具体的看 github : https://github.com/TrueNewBee/pythonDemo 看一下效果图: 整体来说还是 ...