原题链接在这里:https://leetcode.com/problems/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 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". 

题解:

先将p的所有char对应的map count++.

然后快慢指针维护一个sliding window. 右侧runner每次都移动,对应的char的count减1. 若是count减1之前大于0, 表示char在p中, sum--.

sum为0时就找到了anagram.

当window大小为p长度时,移动左侧walker, 对应char的count加1. 若是count加1之前不是负数,表示这个char在p中, sum++.

Time Complexity: O(n). n = s.length().

Space: O(1). 用到了map.

AC Java:

 public class Solution {
public List<Integer> findAnagrams(String s, String p) {
List<Integer> res = new ArrayList<Integer>();
if(s == null || p == null || s.length() == 0 || p.length() == 0){
return res;
}
int [] map = new int[256];
for(int i = 0; i<p.length(); i++){
map[p.charAt(i)]++;
}
int walker = 0;
int runner = 0;
int sum = p.length();
while(runner < s.length()){
if(map[s.charAt(runner++)]-- > 0){
//Move runner everytime. Decrease corresponding char count by 1
//If current char count is larger than 0, then this char is in p
sum--;
} if(sum == 0) {
//Find anagram
res.add(walker);
} if(runner-walker == p.length() && map[s.charAt(walker++)]++ >= 0){
//If windows's size is already p.length(), move walker and increase corresponding char count by 1
//If count before increasing is not smaller than 0, this char is in p
sum++;
}
}
return res;
}
}

类似Permutation in String.

LeetCode Find All Anagrams in a String的更多相关文章

  1. [LeetCode] 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】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 ...

  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@ [49] Group Anagrams (Hashtable)

    https://leetcode.com/problems/anagrams/ Given an array of strings, group anagrams together. For exam ...

  6. LeetCode 49 Group Anagrams(字符串分组)

    题目链接: https://leetcode.com/problems/anagrams/?tab=Description   Problem:给一个字符串数组,将其中的每个字符串进行分组,要求每个分 ...

  7. [Leetcode][Python]49: Anagrams

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 49: Anagramshttps://leetcode.com/proble ...

  8. 438. Find All Anagrams in a String

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

  9. [LeetCode] 151. Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

随机推荐

  1. nginx部分命令

    启动nginx start nginx 停止nginx nginx -s stop 重启nginx nginx -s reload 查看版本信息 nginx -v 大写V是查看配置信息 查看nginx ...

  2. python信号signal简单示例

    进程间通信之类的,用得着, 可以自定义接到信息之后的动作. file1.py #!/usr/bin/env python # -*- coding: utf-8 -*- import os impor ...

  3. SVN-功能介绍之切换

    当初新建项目IMyYa 提交到svn 目录下file:///H:/svn/truck/IMyYa 现在svn 目录版本库中调整为file:///H:/svn/truck/Win8/IMyYa 与之前不 ...

  4. 【Mybatis高级映射】一对一映射、一对多映射、多对多映射

    前言 当我们学习heribnate的时候,也就是SSH框架的网上商城的时候,我们就学习过它对应的高级映射,一对一映射,一对多映射,多对多映射.对于SSM的Mybatis来说,肯定也是差不多的.既然开了 ...

  5. nfs的挂载方法

    对于很多嵌入式驱动开发者,要进行很多次调试,如果nfs搭建不起来,那么对开发是很不方便的.经过三天,我终于把自己编的内核下载到开发板,并实现了nfs文件系统的挂载.今天把过程写下来. 思路 一 编译a ...

  6. Javascript中的apply与call详解

    JavaScript中有一个call和apply方法,其作用基本相同,但也有略微的区别. 一.方法定义 1.call 方法 语法:call([thisObj[,arg1[, arg2[, [,.arg ...

  7. 单色半透明-兼容IE7

    background: #000; width: 100%;height: 100%; filter: alpha(opacity=30); opacity: 0.3;

  8. VS2010 有关测试的一些使用

    Visual Studio 2010 单元测试之一---普通单元测试:http://blog.csdn.net/tjvictor/archive/2011/02/09/6175362.aspx   V ...

  9. iOS 之 Cocoapods安装

    进入正题前,先来点前奏:了解cocoapods是某天看一个博客,那时才明白原来写项目不用一个个将三方库拷进项目里啊,惊讶的我是一塌糊涂的啊...(原谅我那时还没进入过正规的IT公司....好多你们自然 ...

  10. an excellent capability of C# language and compiler

    Sometimes you want to write code that works for different primitive types, and as C# doesn't support ...