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 ...
随机推荐
- [转帖]鲁大师Q3季度PC处理器排行:AMD、Intel终于五五开了
鲁大师Q3季度PC处理器排行:AMD.Intel终于五五开了 https://www.cnbeta.com/articles/tech/902375.htm 近日,鲁大师发布了Q3季度PC处理器排行. ...
- python 之 面向对象(多态性、装饰器方法 内置函数补充)
7.6 多态性 1 什么是多态性 多态指的是同一种事物多种形态,在程序中用继承可以表现出多态.多态性:可以在不用考虑对象具体类型的前提下而直接使用对象下的方法 2.为什要用多态 用基类创建一套统一的规 ...
- 基于openfire的IM即时通讯软件开发
openfire:http://www.igniterealtime.org/ Xmpp:http://xmpp.org/ IOS(xmppframework):https://github.com/ ...
- oracle笔记之计算年龄、工龄和TRUNC
方法一:利用months_between 函数计算 SELECT TRUNC(months_between(sysdate, birthday)/12) AS agefrom dual; 方法二:日期 ...
- 简单添加自己的驱动程序到Linux内核树中
背景: 移植4g模块的时候,看到文档中有添加驱动到内核的步骤,于是趁着这个机会,展开有关的学习. 了解更多可以访问:<Kconfig语法简介> Target :hi3531d Linux ...
- Spring的事件监听ApplicationListener
ApplicationListener是Spring事件机制的一部分,与抽象类ApplicationEvent类配合来完成ApplicationContext的事件机制. 如果容器中存在Applica ...
- Tomcat一闪而过的调试方法
很少用tomcat来部署,都是用springboot微服务.只是以前学的时候搞demo试过而已. 软件测试的期末作业要求要测一个Javaweb的项目,给了一个包然后要求部署在tomcat中并启动. 然 ...
- SS L服务
WebHttpBinding _binding = new WebHttpBinding(); WebServiceHost ws = new WebServiceHost(typeof(Servic ...
- hibernate注解(自动建表如何有表DDL注释) -- Comment用法
import java.io.Serializable; import java.sql.Date; import java.sql.Timestamp; import javax.persisten ...
- R_数据操作_高级_04
数学函数: abs(x) 绝对值 sqrt(x) 平方根 ceiling(x) 放回不小于x的最小整数 floor(x) 不小于x的最大整数 trunc(x) 先0方向截取x的整数部分 ...