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. 数组初始化 和 vector初始化

    ] = {}; 整个数组都初始化为0 vector<); 整个vector初始化为1 如果你定义的vector是这样定义的: vector<int> B; 去初始化,千万不要用: ; ...

  2. Ueditor1.4.3上传视频IE下无法播放的问题

    一:百度编辑器插入视频后,自动生成一段代码: <video class="edui-upload-video vjs-default-skin video-js" contr ...

  3. js基本语法之 值类型(数据类型)(变量类型)

    一.不可改变的原始值(栈数据)(五个) 数字(number),字符串(string),布尔值(boolean),undefined,null 其中;undefined是未定义的意思,而null是空的意 ...

  4. (38)zabbix中配置snmp监控

    1.首先按照“snmp监控快速配置”文本文档在被监控的主机上安装.配置及启动snmp服务, 具体内容如下: 1).安装snmp yum install net-snmp* -y cp -a /etc/ ...

  5. 入门人工智能的首选语言为什么会是Python?

    为何人工智能(AI)首选Python?当你读完这篇文章就会明白了.为何人工智能(AI)首选Python?读完这篇文章你就知道了.我们看谷歌的TensorFlow基本上所有的代码都是C++和Python ...

  6. (原) 剑指offer--之数值的整数次方

    题目描述 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方.   初次看题觉得这题好简单,直接用库函数power()不就行了,仔细想了想,万 ...

  7. PAT Basic 1023

    1023 组个最小数 给定数字0-9各若干个.你可以以任意顺序排列这些数字,但必须全部使用.目标是使得最后得到的数尽可能小(注意0不能做首位).例如:给定两个0,两个1,三个5,一个8,我们得到的最小 ...

  8. Mysql 使用命令及 sql 语句示例

    Mysql 是数据库开发使用的主要平台之一.sql 的学习掌握与使用是数据库开发的基础,此处展示详细sql 语句的写法,及各种功能下的 sql 语句. 在此处有 sql 语句使用示例:在这里 此处插入 ...

  9. 【03】图解原型和原型链by魔芋

    [03]图解原型和原型链 一图胜前言             请先结合图解原型和原型链这张图. 可以分为4种情况. 情况1: Object有: constructor:是Function. __pro ...

  10. BRVAH(让RecyclerView变得更高效)(1)

    本文来自网易云社区 作者:吴思博 对于RecyclerView, 我们重复编写着那一个又一个的列表界面,有的要分组,有的要添加广告头部.有的要不同类型item排列.等等需求,主要代码有大部分是重复的, ...