【leetcode刷题笔记】Substring with Concatenation of All Words
You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening characters.
For example, given:
S: "barfoothefoobarman"
L: ["foo", "bar"]
You should return the indices: [0,9].
(order does not matter).
题解:题目的意思是在S中找到一个子串,恰好包含了L中所有的串,L中的串在S的字串中的顺序不重要。
思路很简单,假设L中共有m个串,每个串长度为n,那么L中子串合并起来总长度是m*n,那么只要在S中依次搜索长度为m*n的串就可以了。在搜索的过程中,设置两个hashmap,一个存放L中的串和它们在L中出现的次数,一个存放在S中m*n的子串中找到的长度为n的串和它们在S的子串中出现的次数,因为查看的是S长度为m*n的子串,并且是n个字符为一组查看的,所以要么在S中看到某个长度为n的子串不出现在L中,要么在S中出现的次数比L中多,否则这个长度为m*n的串就是L的所有串的合并。
例如题目中的例子
- 我们首先查看S的子串barfoo,查看这个子串的时候,按照bar,foo的顺序查看,得知子串foobar是符合要求的
- 再查看子串arfoot,查看顺序是arf,oot,发现arf不在L中,所以arfoot不符合要求;
- 再查看子串rfooth,......
if(L == null || L.length == 0)
return null;
int m = L.length;
int n = L[0].length();
//store n-length strings in L
HashMap<String, Integer> map = new HashMap<String, Integer>();
//store n-length strings inS
HashMap<String, Integer> InS = new HashMap<String, Integer>();
List<Integer> answer = new ArrayList<Integer>();
for(String s:L){
if(!map.containsKey(s))
map.put(s, 1);
else {
map.put(s, map.get(s)+1);
}
} for(int i = 0;i <= S.length() - m*n;i++){
InS.clear();
boolean find = true;
for(int j = 0;j < m;j++){
String sub = S.substring(i+j*n,i+(j+1)*n);
//if a n-length string in S's substring doesn't in L, skip to search a new substring in S
if(!map.containsKey(sub)){
find = false;
break;
}
if(!InS.containsKey(sub))
InS.put(sub, 1);
else {
InS.put(sub, InS.get(sub)+1);
}
//if a n-length string in S'substring appears more time than in L, stop checking this substring
if(InS.get(sub) > map.get(sub)){
find = false;
break;
}
}
if(find)
answer.add(i);
}
return answer;
【leetcode刷题笔记】Substring with Concatenation of All Words的更多相关文章
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- 18.9.10 LeetCode刷题笔记
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...
- LeetCode刷题笔记 - 12. 整数转罗马数字
学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...
- Leetcode刷题笔记(双指针)
1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...
- (python)leetcode刷题笔记03 Longest Substring Without Repeating Characters
3. Longest Substring Without Repeating Characters Given a string, find the length of the longest sub ...
- (python)leetcode刷题笔记05 Longest Palindromic Substring
5. Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You ...
- 【leetcode刷题笔记】Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- 【leetcode刷题笔记】Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- LeetCode刷题笔记(1-9)
LeetCode1-9 本文更多是作为一个习题笔记,没有太多讲解 1.两数之和 题目请点击链接 ↑ 最先想到暴力解法,直接双循环,但是这样复杂度为n平方 public int[] twoSum(int ...
随机推荐
- 【BZOJ4008】【HNOI2015】亚瑟王 概率DP
链接: #include <stdio.h> int main() { puts("转载请注明出处[辗转山河弋流歌 by 空灰冰魂]谢谢"); puts("网 ...
- Matlab中使用jython扩展功能
Matlab中面向对象能力并不强,通过使用jython引擎能够对其功能扩展. 1 编辑classpath.txt增加jython.jar 在matlab中输入 which classpath.txt ...
- Frogger - poj 2253 (Dijkstra)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28802 Accepted: 9353 Description Fr ...
- 关于Java静态代码块、初始化块、构造函数的调用顺寻问题?
public class ClassA { public ClassA(){ System.out.println("A 构造..."); } { System.out.print ...
- 几种session存储方式比较
原文: http://blog.sina.com.cn/s/blog_495697e6010143tj.html 集群中session安全和同步是个最大的问题,下面是我收集到的几种session同步的 ...
- Unity3D研究院之在开始学习拓展编辑器
Unity拥有非常丰富的拓展编辑器接口,如果是在网上下载过别人写的插件,你会发现为什么它的监测面板视图和普通的不一样?其实是他通过代码自己绘制的监测面板,这篇博文MOMO带大家来学习编辑器.如下图所示 ...
- Cow Contest(传递闭包)
Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10450 Accepted: 5841 Desc ...
- Computer Transformation(简单数学题+大数)
H - Computer Transformation Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &am ...
- hdu 4414 Finding crosses【简单模拟】
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4414 CSUST:点击打开链接 Finding crosses Time Limit: 2000/1000 ...
- 6.2.1-FactoryBeanRegistrySupport(未全)
FactoryBeanRegistrySupport 的关系图: 添加工厂方式创建类FactoryBean的支持