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. HTTP、HTTP1.0、HTTP1.1、HTTP2.0——笔记

    笔记来源地址:https://mp.weixin.qq.com/s/T2IErLDxbWP1a-VbRkZZHg HTTP: HTTP是WWW数据通信的基础,是应用层协议. HTTP是干什么的?用来给 ...

  2. Angular——单页面实例

    基本介绍 1.引入的route模块可以对路由的变化做出响应 2.创建的控制器中依然需要$http向后台请求数据 3.php中二维数据的遍历用的是foreach 4.php中$arr=array(),$ ...

  3. Verilog之event

    1 Explicit event The value changes on nets and variable can be used as events to trigger the executi ...

  4. dubbo之日志适配及访问日志

    日志适配 自 2.2.1 开始,dubbo 开始内置 log4j.slf4j.jcl.jdk 这些日志框架的适配 1,也可以通过以下方式显示配置日志输出策略: 命令行 java -Ddubbo.app ...

  5. 网络编程基础_3.APC队列

    APC队列 #include <stdio.h> #include <windows.h> // 保存 IO 操作的结果 CHAR Buffer1[] = { }; CHAR ...

  6. java_IO_2

    1.字节流  InputStream(抽象类) package ioStudy; import java.io.File; import java.io.FileInputStream; import ...

  7. position的简单用法实例 ----- 方框里图片放对应的角标

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...

  8. Autowired和Resource的区别

    Autowired是属于spring的注解,默认按类型装配,且依赖对象必须存在,如果允许为null,需要设置Autowired(required=false)   Resource属于javax,默认 ...

  9. enote笔记语言(2)(ver0.5)

    why not(whyn't)                    为什么不(与“why”相反对应,是它的反面.它的矛盾对立面)   how对策 how设计   key-memo:         ...

  10. SpringMVC中@Controller和@RequestMapping用法和其他常用注解(转)

    一.简介 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ,然后再把该Mo ...