Substring Anagrams
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 40,000.
The order of output does not matter.
Example
Given s = "cbaebabacd" p = "abc"
return [0, 6]
The substring with start index = 0 is "cba", which is an anagram of "abc".思路: HashTable
The substring with start index = 6 is "bac", which is an anagram of "abc".
RelatedProblems : Anagrams Two Strings Are Anagrams
public class Solution {
/**
* @param s a string
* @param p a non-empty string
* @return a list of index
*/
public List<Integer> findAnagrams(String s, String p) {
List<Integer> result = new ArrayList<>();
if (s.length() < p.length()) {
return result;
}
int[] numS = new int[256];
int[] numP = new int[256];
for (int i = 0; i < p.length(); i++) {
numS[s.charAt(i) - 'a']++;
numP[p.charAt(i) - 'a']++;
}
if (Arrays.equals(numS,numP)) {
result.add(0);
}
for (int i = p.length(); i < s.length(); i++) {
numS[s.charAt(i) - 'a']++;
numS[s.charAt(i - p.length()) - 'a']--;
if (Arrays.equals(numS,numP)) {
result.add(i - p.length() + 1);
}
}
return result;
}
}
Substring Anagrams的更多相关文章
- 子串字谜substring anagrams
[抄题]: 给定一个字符串 s 和一个 非空字符串 p ,找到在 s 中所有关于 p 的字谜的起始索引.字符串仅由小写英文字母组成,字符串 s 和 p 的长度不得大于 40,000.输出顺序无关紧要. ...
- 第三章 基础算法和数据结构高频题 I
区间类问题 1 Missing Interval public List<String> findMissingRanges(int[] nums, int lower, int uppe ...
- [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 ...
- LeetCode Find All Anagrams in a String
原题链接在这里:https://leetcode.com/problems/find-all-anagrams-in-a-string/ 题目: Given a string s and a non- ...
- leetcode_438_Find All Anagrams in a String_哈希表_java实现
题目: Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Stri ...
- [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 c ...
- [Swift]LeetCode438. 找到字符串中所有字母异位词 | 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 ...
- [LeetCode] 438. Find All Anagrams in a String_Easy
438. Find All Anagrams in a String DescriptionHintsSubmissionsDiscussSolution Pick One Given a str ...
- [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 ...
随机推荐
- 长乐培训Day8
T1 远征 题目 [题目描述] 寒枫将军将要带领他的部队去圣雪山消灭那里的冰龙.部队分成了若干个小队,属于同一个小队的人兵种相同. 寒枫将军有着杰出的指挥能力,在战斗的时候,寒枫将军能够让所有相同兵种 ...
- python — 线程
目录 1.线程基础知识 2 Thread 类 3 锁 4 队列 1.线程基础知识 1.1 进程与线程的区别 进程: 创建进程 时间开销大 销毁进程 时间开销大 进程之间切换 时间开销大 线程: 线程是 ...
- Linux安装Python3流程
安装必要的依赖库文件 yum -y install zlib zlib-devel bzip2 bzip2-devel ncurses ncurses-devel readline readline- ...
- vue 的 solt 子组件过滤
如上图: 1.定义了一个类似下拉的组件 mySelect , 然后里面有自定义的组件 myOptions 2.有很多时候,我们希望, mySelect 组件内部的子组件,只能是 myOptions . ...
- dotnet core2.2 通过虚拟机发布到CentOS上
自从.net core出现的时候,就知道c#的代码居然能后运行到Linux上面,以前都没想过居然这么牛逼,所以很早就想学习怎样部署上去,直到现在.net core都出现2.2了,才花时间去接触,说实话 ...
- jQuery的显示和隐藏
在 jQuery 中可以使用 hide() 和 show() 方法来隐藏和显示 HTML 元素,以及使用 toggle() 方法能够切换 hide() 和 show() 方法. 隐藏例子: <! ...
- css常用代码大全
css常用代码大全,html+css代码 html+css可以很方便的进行网页的排版布局,还能减少很多不必要的代码. 一.文本设置1.font-size: 字号参数 2.font-style: 字体格 ...
- mysql(单表查询,多表查询,MySQl创建用户和授权,可视化工具Navicat的使用)
单表查询 语法: 一.单表查询的语法 SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT ...
- unittest 运行slenium(三)---通过数据驱动形式运行用例
一: 获取数据 获取用例所在位置,通过OpenExcelPandas来读取用例里面的全部数据.通过某个列名来创建新的序号. 并将结果转换成list类型,将其作为ddt数据的来源. 1. 在test文 ...
- Qt中C++与QML交互
###main.c部分int main(int argc, char *argv[]){ QString info1 = "xxxxxxxxxxx"; QString ...