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 ...
随机推荐
- LeetCode 14. 最长公共前缀(Longest Common Prefix)
14. 最长公共前缀 14. Longest Common Prefix 题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". Lee ...
- Python02之continue,break语句
Python中的break和continue用法基本一样 break和continue都是用在while和for循环中,而不是跳出if...elif..else的判断语句中,跳出是直接跳出语句所在的w ...
- 15-16 ICPC europe J Saint John Festival (graham扫描法+旋转卡壳)
题意:给n个大点,m个小点$(n<=1e5,m<=5e5),问有多少个小点,存在3个大点,使小点在三个大点组成的三角形内. 解题思路: 首先,易证,若该小点在某三大点行成的三角形内,则该小 ...
- 18 JSON、JSON字符串、反序列化
JSON教程 : https://www.runoob.com/python/python-json.html 概念 JSON是一种轻量级的数据交换格式,它是一种数据格式! JSON易于阅读.易于解析 ...
- C/C++中内存泄漏、内存溢出与野指针的解释与说明
1.内存泄漏 内存泄漏是指我们在堆中申请(new/malloc)了一块内存,但是没有去手动的释放(delete/free)内存,导致指针已经消失,而指针指向的东西还在,已经不能控制这块内存, 所以就是 ...
- C/C++读写文件的几种方法fstream fopen、fwrite()、fread()操作
C中采用的主要是文件指针的办法,C++中对文件的操作主要运用了"文件流"(即非标准的输入输出)的思想 c读写文件fopen C 库函数 FILE *fopen(const char ...
- 【转载】 C#使用Union方法求两个List集合的并集数据
在C#语言的编程开发中,有时候需要对List集合数据进行运算,如对两个List集合进行交集运算或者并集运算,其中针对2个List集合的并集运算,可以使用Union方法来快速实现,Union方法的调用格 ...
- 攻防世界(Ctf-Web 新手练习区 Writeup)
平台地址:adworld.xctf.org.cn 在打着暑假工赚零花钱之余,我将这些题利用空余时间刷了一遍,感觉内心还是比较满足的! 题目:view_source 这道题没啥好说的,在url的前面加上 ...
- js几种数组排序及sort的实现
给出以下数组,并进行排序处理 var arr = new Array('1','3','8','2','3','5'); 1. 插入法排序 Array.prototype.csSort = funct ...
- JAVA笔记整理(一),JAVA介绍
JAVA语言的版本: J2SE(Java2 Platform Standard Edition,java平台标准版),后更名为:JAVA SE J2EE(Java 2 Platform,Enterpr ...