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. Selenium基于Python web自动化测试框架 -- PO

    关于selenium测试框架首先想到的就是PO模型,简单说下PO模型 PO模型的概念和理解: PO就是一个设计思想,将代码以页面为单位进行组织,针对这个页面上的所有信息.相关操作都放到一个类中,从而使 ...

  2. python对象以及pickle腌制

    #python对象 1.什么是python的对象 2.详解pickle腌制 1.什么是python的对象 Python的内置的对象类型主要有数字.字符串.列表.元组.字典.集合等等.其实,在pytho ...

  3. 最优化方法系列:Adam+SGD—>AMSGrad

    自动调参的Adam方法已经非常给力了,不过这主要流行于工程界,在大多数科学实验室中,模型调参依然使用了传统的SGD方法,在SGD基础上增加各类学习率的主动控制,以达到对复杂模型的精细调参,以达到刷出最 ...

  4. LR性能分析随笔(一)

    一.关键词 吞吐量:对于吞吐量,单位时间内吞吐量越大,说明服务器的处理能力越好:而请求数仅表示客户端向服务器发出的请求数,与吞吐量一般成正比关系. HTTP:HTTP404表示文件或目录没有找到.有些 ...

  5. v-bind、v-on、计算属性

    v-bind 缩写 <!-- 完整语法 --> <a v-bind:href="url">...</a> <!-- 缩写 --> & ...

  6. ls 命令还能这么玩?看一下这 20 个实用范例

    Linux中一个基本命令是ls.没有这个命令,我们会在浏览目录条目时会遇到困难.这个命令必须被每个学习Linux的人知道. ls是什么 ls命令用于列出文件和目录.默认上,他会列出当前目录的内容.带上 ...

  7. tf idf公式及sklearn中TfidfVectorizer

    在文本挖掘预处理之向量化与Hash Trick中我们讲到在文本挖掘的预处理中,向量化之后一般都伴随着TF-IDF的处理,那么什么是TF-IDF,为什么一般我们要加这一步预处理呢?这里就对TF-IDF的 ...

  8. bzoj 4026 dC Loves Number Theory 主席树+欧拉函数

    题目描述 dC 在秒了BZOJ 上所有的数论题后,感觉萌萌哒,想出了这么一道水题,来拯救日益枯竭的水题资源.给定一个长度为 n的正整数序列A,有q次询问,每次询问一段区间内所有元素乘积的φ(φ(n)代 ...

  9. mysql如何将一个字段多个类型串成一个字符串?

    结论 先说结论,可以使用group_concat group by的组合实现多行变一行,将一个字段的多个类型串成一个字段 需求: 如题,一个字段如电影类别,一部电影可以是多个类别,如喜剧.动作片等,其 ...

  10. 怎么提交小程序给微信?微信小程序的提交审核流程

    开发者开发好一款微信小程序后,如何将其提交给微信审核呢?今天正好有空,就整理了一下小程序的提交流程,以供大家参考.如果要发布小程序,那么你需要申请真正的小程序账号,拿到appId,才能在手机预览.及提 ...