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". 本题开始的时候并没有想用O(n)的时间复杂度做出来,而是用了O(mn)的时间复杂度做,看了答案才知道本题是sliding window的变体,是two pointer的例子,不是很难,代码如下:
 public class Solution {
public List<Integer> findAnagrams(String s, String p) {
List<Integer> res = new ArrayList<Integer>();
if(s==null||s.length()==0||p==null||p.length()==0) return res;
int left = 0,right = 0;
int count =p.length();
int[] word = new int[256];
for(int i=0;i<p.length();i++){
word[p.charAt(i)]++;
}
while(right<s.length()){
if(word[s.charAt(right++)]-->=1) count--;
if(count==0) res.add(left);
if(right-left==p.length()&&word[s.charAt(left++)]++>=0) count++;
}
return res;
}
}

438. Find All Anagrams in a Strin的更多相关文章

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

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

  2. 438. Find All Anagrams in a String

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

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

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

  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 找出字符串中所有的变位词

    Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings ...

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

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

  8. [LC] 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 ...

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

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

随机推荐

  1. Jordan 标准型定理

    将学习到什么 就算两个矩阵有相同的特征多项式,它们也有可能不相似,那么如何判断两个矩阵是相似的?答案是它们有一样的 Jordan 标准型.   Jordan 标准型定理 这节目的:证明每个复矩阵都与一 ...

  2. 验证IP端与数据库Ip端是否重复!!!

    select COUNT(id) from house_info_config hic where (hic.ip_start <![CDATA[<=]]> #{ipend} AND ...

  3. 使用Auto Layout中的VFL(Visual format language)--代码实现自动布局

    使用Auto Layout中的VFL(Visual format language)--代码实现自动布局 2014-12-09 10:56 编辑: zhiwupei 分类:iOS开发 来源:机智的新手 ...

  4. hihoCoder-1098-kruskal

    如果起始点和终止点的父节点相同,就说明它们就已经在同一个连通分量里面,说明,起始点和终止点在此之前就已经被连入同一个分量之中,如果此时还将起始点和终止点连入此分量,就会形成回路,想象一个三角形,你大概 ...

  5. ZOJ Monthly, January 2019-Little Sub and Pascal's Triangle

    这个题的话,它每行奇数的个数等于该行行号,如果是0开始的,就该数的二进制中的1的个数,设为k,以它作为次数,2k就是了. #include <stdio.h> int main() { i ...

  6. C#图形学习笔记

    绘图常用控件.类和结构 颜色 使用System.Drawing.Color结构表示 设置颜色的方法 调用静态函数:Color.FromArgb() public static Color FromAr ...

  7. 蓝牙学习 (8)配对raspberryPi和SensorTag CC2650

    在上一篇中,用raspberryPi能够扫描到Ti SensorTag. 但是没有获得更多的数据,并且发现sensor Tag并没有回复scan request. https://blog.csdn. ...

  8. python爬虫基础02-urllib库

    Python网络请求urllib和urllib3详解 urllib是Python中请求url连接的官方标准库,在Python2中主要为urllib和urllib2,在Python3中整合成了urlli ...

  9. python 游戏(滑动拼图Slide_Puzzle)

    1. 游戏功能和流程图 实现16宫格滑动拼图,实现3个按钮(重置用户操作,重新开始游戏,解密游戏),后续难度,额外添加重置一次的按钮,解密算法的植入,数字改变为图片植入 游戏流程图 2. 游戏配置 配 ...

  10. Canal的安装与使用

    一.Canal介绍 Canal的原理就是它自己伪装成slave, 向mysql发送dump协议,MySQL master接收到dump请求之后推送binlog文件给slave, 也就是canal. 二 ...