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

题目标签:Hash Table

  题目给了我们两个string s 和 p, 让我们在 s 中 找到所有 p 的变位词。

  利用两个HashMap 和 Sliding window:

    先把 p 的char 和 出现次数 存入 mapP;

    然后遍历string s,利用 sliding window 把 和 p 一样长度的 string 的 char 保存在 tempMap 里,比较 tempMap 和 mapP。

Java Solution:

Runtime beats 20.00%

完成日期:11/07/2017

关键词:HashMap

关键点:利用sliding window 把 tempMap 和 mapP 比较

 class Solution
{
public List<Integer> findAnagrams(String s, String p)
{
List<Integer> list = new ArrayList<>();
HashMap<Character, Integer> mapP = new HashMap<>();
HashMap<Character, Integer> tempMap = new HashMap<>(); for(char c: p.toCharArray()) // store p char and occurrence into mapP
mapP.put(c, mapP.getOrDefault(c, 0) + 1); for(int i=0; i<s.length(); i++) // iterate s and update a tempMap with p len
{
char c = s.charAt(i);
char leftC; tempMap.put(c, tempMap.getOrDefault(c, 0) + 1); if(i >= p.length()) // once reach to p's length, remove most left char
{
leftC = s.charAt(i - p.length());
// remove left char
if(tempMap.get(leftC) == 1)
tempMap.remove(leftC);
else
tempMap.put(leftC, tempMap.get(leftC) - 1);
} if(tempMap.equals(mapP))
list.add(i + 1 - p.length()); } return list;
}
}

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

LeetCode 438. Find All Anagrams in a String (在字符串中找到所有的变位词)的更多相关文章

  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. 438. Find All Anagrams in a String - LeetCode

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

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

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

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

  6. 438. Find All Anagrams in a String

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

  7. [LeetCode] 438. Find All Anagrams in a String_Easy

    438. Find All Anagrams in a String DescriptionHintsSubmissionsDiscussSolution   Pick One Given a str ...

  8. hiho1482出勤记录II(string类字符串中查找字符串,库函数的应用)

    string类中有很多好用的函数,这里介绍在string类字符串中查找字符串的函数. string类字符串中查找字符串一般可以用: 1.s.find(s1)函数,从前往后查找与目标字符串匹配的第一个位 ...

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

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

随机推荐

  1. 安装好Pycharm后如何配置Python解释器简易教程

    呃呃,遇到坑了...... 安装完Python,没有去配置好Python解释器,直接打开Python项目包,去运行程序,程序输出结果只是显示 Process finished with exit co ...

  2. 413 Request Entity Too Large报错处理

    修改nginx配置   这是最简单的一个做法,着报错原因是nginx不允许上传配置过大的文件,那么件把nginx的上传大小配置调高就好.    1.打开nginx主配置文件nginx.conf,一般在 ...

  3. ANNOTATION and analyse hello1.java

    一.What is annotation? annotation的中文意思就是注解,注释的意思.注解也属于一种类型.它是在 Java SE 5.0 版本中开始引入的概念.它的形式跟接口很类似,不过前面 ...

  4. Deepin系统关于每次启动终端都要输入source /etc/profile的问题

    关于每次启动终端都要输入source /etc/profile的问题 当我在Deepin系统中下载了node以及npm之后,我为了将node导入到系统文件,使用了以下命令sudo gedit ``/e ...

  5. Windows下运行jekyll,编码已不再是问题

    很久没更新jekyll了,所以好奇着去官网看了下更新记录,发现如下更新条目(版本1.3.0/2013-11-04发布): Add encoding configuration option (#144 ...

  6. [Luogu] P3258 [JLOI2014]松鼠的新家

    题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真的住在”树“上. 松鼠想邀请小熊维尼前 ...

  7. 手机端--tap PC端--click

    区别: tap为jq mobile 的方法 1.click与tap都会触发点击事件,但是在手机web端,click会有200-300ms的延迟,所以一般用tap代替click作为点击事件.single ...

  8. Linux设置history命令显示行数以及时间

    Linux和unix上都提供了history命令,可以查询以前执行的命令历史记录但是,这个记录并不包含时间项目因此只能看到命令,但是不知道什么时间执行的如何让history记录时间呢? 解决方案 注意 ...

  9. Python面向对象一些阶段性总结

    什么时候用面向对象??? 1.共同的方法,提出来.共同的方法可以产生一个连接,其他方法可以使用,例如传文件,和执行命令都需要建立连接,关闭连接 例如 获取连接,...操作..关闭连接 2.创建模板.造 ...

  10. LearnPython笔记:ex48 代码

    赶紧写上 ,一定有人着急要看,啊哈哈哈哈,嘻嘻 哈哈 不枉我起了个大早 利用什么碎片时间啊,真正能深入学习的,是需要大段大段不被打断的时间 1. 完全实现了如下几种输入数据: 2. 遗留:最后一个el ...