作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/find-all-anagrams-in-a-string/description/

题目描述

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

题目大意

在s中有多少个位置,从这个位置开始和p等长度的子字符串中,所包含的字符和p是一样的。

解题方法

滑动窗口

这个题考的是时间复杂度。如果判断两个切片是否是排列组合的话,时间复杂度略高,会超时。

能AC的做法是用了一个滑动窗口,每次进入窗口的字符的个数+1,超出滑动窗口的字符个数-1.

这样就一遍就搞定了,而且不用每个切片都算是不是一个排列组合。

Counter大法好,判断两个字符串是否是排列组合直接统计词频然后==判断即可。

注意如果一个词出现的次数是0,那么需要从Counter中移除,因为Counter({‘a’: 0, ‘b’: 1}) w不等于Counter({‘b’: 1})。

from collections import Counter
class Solution(object):
def findAnagrams(self, s, p):
"""
:type s: str
:type p: str
:rtype: List[int]
"""
answer = []
m, n = len(s), len(p)
if m < n:
return answer
pCounter = Counter(p)
sCounter = Counter(s[:n-1])
index = 0
for index in xrange(n - 1, m):
sCounter[s[index]] += 1
if sCounter == pCounter:
answer.append(index - n + 1)
sCounter[s[index - n + 1]] -= 1
if sCounter[s[index - n + 1]] == 0:
del sCounter[s[index - n + 1]]
return answer

双指针

二刷的时候也是滑动窗口,但是使用的是双指针的解法,看上去好像没有上面这个方法更简单。

class Solution(object):
def findAnagrams(self, s, p):
"""
:type s: str
:type p: str
:rtype: List[int]
"""
count = collections.Counter()
M, N = len(s), len(p)
left, right = 0, 0
pcount = collections.Counter(p)
res = []
while right < M:
count[s[right]] += 1
if right - left + 1 == N:
if count == pcount:
res.append(left)
count[s[left]] -= 1
if count[s[left]] == 0:
del count[s[left]]
left += 1
right += 1
return res

日期

2018 年 1 月 27 日
2018 年 11 月 24 日 —— 周六快乐

【LeetCode】438. Find All Anagrams in a String 解题报告(Python)的更多相关文章

  1. 【LeetCode】833. Find And Replace in String 解题报告(Python)

    [LeetCode]833. Find And Replace in String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...

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

  5. 【LeetCode】1065. Index Pairs of a String 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

  6. 【LeetCode】345. Reverse Vowels of a String 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用栈 双指针 日期 [LeetCode] 题目地址 ...

  7. 【LeetCode】777. Swap Adjacent in LR String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 智商题 相似题目 参考资料 日期 题目地址:http ...

  8. 【LeetCode】467. Unique Substrings in Wraparound String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/unique-s ...

  9. LeetCode 806 Number of Lines To Write String 解题报告

    题目要求 We are to write the letters of a given string S, from left to right into lines. Each line has m ...

随机推荐

  1. Notepad++—显示代码对齐是使用了制表符还是空格

    使用Notepad++打开脚本,勾选"显示空格与制表符",此时你会看到代码对齐使用了制表符与空格 右箭头:TAB:空格:点: 参考:https://www.cnblogs.com/ ...

  2. Linux—linux 查看一个文件有多少M

    ls -l --block-size=M   #就把目录下的所有文件按M单位呈现

  3. Spark3学习【基于Java】3. Spark-Sql常用API

    学习一门开源技术一般有两种入门方法,一种是去看官网文档,比如Getting Started - Spark 3.2.0 Documentation (apache.org),另一种是去看官网的例子,也 ...

  4. 在C++的map类型中按value排序

    1.将map转化为vector类型 2.使用sort函数对vector进行排序,写出compare比较器函数 3.比较器中指明按照第几个元素来排序 1 #include <iostream> ...

  5. 19. awk 命令详解

    awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各 ...

  6. Azkaban(二)【WorkFlow案例实操】

    目录 1.使用步骤 2.案例: 1.hello word 2.作业依赖[dependsOn配置作业的依赖关系] 3.内嵌工作流 4.全局配置 [在开头通过config进行配置,后续可以通过${属性名} ...

  7. 【Spring Framework】12种spring中定义bean的方法

    前言 在庞大的java体系中,spring有着举足轻重的地位,它给每位开发者带来了极大的便利和惊喜.我们都知道spring是创建和管理bean的工厂,它提供了多种定义bean的方式,能够满足我们日常工 ...

  8. Zookeeper客户端链接

    一.zkCli.sh ./zkCli.sh -server 39.97.176.160:2182 39.97.176.160 : zookeeper服务器Ip 2182:zookeeper端口 二.Z ...

  9. springmvc资源文件访问不到,undefined,jsp引用js文件目录

    资源访问失败: 该模块下springmvc.xml文件中添加配置: <mvc:resources mapping="/js/**" location="/js/&q ...

  10. ssm+ajax实现登陆

    ssm的搭建见上一章 1.数据协议层 public User selectByLoginnameAndPassword(@Param("loginname")String logi ...