10.Find All Anagrams in a String(在一个字符串中发现所有的目标串排列)
Level:
Easy
题目描述:
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 sand 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".
思路分析:
知识点:滑动窗口
关键点一:像这种找子串的可以使用滑动窗口Sliding Window来做。这个窗口由left和right来约束[left,right]这个闭区间,刚开始知识left=right=0,然后left不变,right不断增长,直到到达某个值,left和right一起增长,这样就实现了窗口的创建和向下滑动。
关键点二:对于p中出现的字符以及次数需要记下来。可以使用一个数组ascaiiNums,长度为256,是因为ascaii一共只有256个,初始化的值表示角标对应的ascaii字符在p中出现的次数,那么没有出现过的就是0.之后如果s中出现过该字符,那么数组对应位置的值就减一。如果减之后的值大于等于0,就说明p中该字符出现了一次,这时p中未出现的字符数量count就减一。之所以要判断是>=0,是因为如果数组中原来的值是0(说明p中没有出现),那么减一后就成了负的,这种情况下p中剩下未出现的字符的count数值保持不变。如果count的值为0,说明p中的字符全出现了,找到一个符合条件的子串,left就是子串的首地址。之后要向右滑动窗口,移动的方法是left++,right++,更新p中未出现的字符个数count。
代码:
public class Solution{
public List<Integer>findAnagrams(String s,String p){
ArrayList<Integer>res=new ArrayList<>();
if(s==null||p==null||p.length()>s.length())
return res;
int []map=new int [256]; //记录p中出现的元素
for(char c:p.toCharArray())
map[c]++; //以c的Ascall为下标的值加一,其余没出现的都为0;
int count=p.length(); //记录p中还未出现的字符个数
int left=0; //窗口左指针
int right=0; //窗口右指针
while(right<s.length()){
char ch=s.charAt(right);
map[ch]--;
if(map[ch]>=0) //证明ch是p中的字符
count--; //p中未出现的个数减一
if(right-left+1==p.length()){
if(count==0) //说明p中的字符都已经出现
res.add(left);
char ch1=s.charAt(left);
map[ch1]++;
if(map[ch1]>0)//证明当前窗口最左边的值在p中存在,那么由于窗口要向下滑动一位,会舍弃该值,则count的数目就应该加1
count++;
left++;//窗口向下滑动
right++;
}else{
right++;
}
}
return res;
}
}
10.Find All Anagrams in a String(在一个字符串中发现所有的目标串排列)的更多相关文章
- Leetcode438.Find All Anagrams in a String找到字符串中所有字母异位词
给定一个字符串 s 和一个非空字符串 p,找到 s 中所有是 p 的字母异位词的子串,返回这些子串的起始索引. 字符串只包含小写英文字母,并且字符串 s 和 p 的长度都不超过 20100. 说明: ...
- *438. Find All Anagrams in a String 找到字符串中所有字母异位词
1. 原始题目 给定一个字符串 s 和一个非空字符串 p,找到 s 中所有是 p 的字母异位词的子串,返回这些子串的起始索引. 字符串只包含小写英文字母,并且字符串 s 和 p 的长度都不超过 201 ...
- hiho1482出勤记录II(string类字符串中查找字符串,库函数的应用)
string类中有很多好用的函数,这里介绍在string类字符串中查找字符串的函数. string类字符串中查找字符串一般可以用: 1.s.find(s1)函数,从前往后查找与目标字符串匹配的第一个位 ...
- 【编程题目】在一个字符串中找到第一个只出现一次的字符。如输入 abaccdeff,则输出 b。
第 17 题(字符串):题目:在一个字符串中找到第一个只出现一次的字符.如输入 abaccdeff,则输出 b. 思路:此题非常容易. 最开始是想开辟一块空间存储每个字符出现的次数. 但转念一想,似乎 ...
- 1.3 正则表达式和Python语言-1.3.5使用 search()在一个字符串中查找模式(搜索与匹配 的对比)
1.3.5 使用 search()在一个字符串中查找模式(搜索与匹配的对比) 其实,想要搜索的模式出现在一个字符串中间部分的概率,远大于出现在字符串起始部分的概率.这也就是 search()派上用场的 ...
- php如何利用标准输入输出实现在一个字符串中计算某个字符出现的个数?
php如何利用标准输入输出实现在一个字符串中计算某个字符出现的个数? 一.总结 php实现计算字符个数(php标准输入和输出:fgets(STDIN) echo $output;) 1.php标准输 ...
- [LeetCode] Unique Substrings in Wraparound String 封装字符串中的独特子字符串
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母
Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...
- [LeetCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
随机推荐
- mac 下 配置appium +ios真机环境
mac系统:10.11.6 xcode:7 appium:1.5.3 iphone: 6 p 1.搭建 appium 安卓的环境: 1.jdk 2.sdk 3.appium 4.配置环境变量 mac下 ...
- Don’t panic, it’s just a kernel panic (ZT)
http://blog.kreyolys.com/2011/03/17/no-panic-its-just-a-kernel-panic/ One of the main young sysadmin ...
- Dataguard ORA-19909 ORA-01110
在创建ORACLE 10G Dataguard时,报错: Datafile 1 (ckpscn 24967685451) is orphaned on incarnation#=6 MRP0: Bac ...
- Spring4新的javaConfig注解
1.@RestController spring4为了更方便的支持restfull应用的开发,新增了RestController的注解,比Controller注解多的功能就是给底下的RequestMa ...
- python中的整数、浮点数和布尔值
整数和浮点数有那个四则运算: 两种类型的数可以直接进行加减,当整数和浮点数进行加减的时候,结果会自动的变为浮点数,其中除法运算是“/”来表示的, 而余数的算术符号是“%”来表示的. 在布尔值的判断中我 ...
- 开源项目weiciyuan运行前gradle调试过程记录
折腾了几个小时,终于能成功运行了,由于该项目使用的gradle版本过旧,需要做一些调整,具体如下 1.将使用gradle版本号改为你现在用的 2.将build tools版本号改为同上 3.将defa ...
- dubbo-admin打包和zookper安装
1 首选安装Zookper,下载zookeeper-3.5.3-beta版本,在这里我主要演示这个:下载地址:http://mirrors.hust.edu.cn/apache/zookeeper/ ...
- js 操作属性,操作内容,
disable=“disable” 让按钮变得不可选 先建一个按钮,让class = ’btn‘ 然后, 添加,修改属性 document.getElementsByClassName('btn')[ ...
- Java-马士兵设计模式学习笔记-工厂模式-抽象工厂模式
一.概述 1.抽象工厂:当情况是需要产生一系列产品,若需更换产品,则要求一系列产品一起换,且要控制一系列产品的产生过程,此时可考虑抽象工厂模式.例:小明装修屋子,把电视.冰箱都替换掉,他这次需要把电视 ...
- java全栈day04--方法
day04内容介绍 1 方法基础知识 2 方法高级内容 3 方法案例 一 方法的概念 A:为什么要有方法 提高代码的复用性 B 什么是方法 完成特定功能的代码块 修饰符 返回值类型 方 ...