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的更多相关文章

  1. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  2. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

  3. LeetCode刷题笔记 - 12. 整数转罗马数字

    学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...

  4. Leetcode刷题笔记(双指针)

    1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...

  5. (python)leetcode刷题笔记03 Longest Substring Without Repeating Characters

    3. Longest Substring Without Repeating Characters Given a string, find the length of the longest sub ...

  6. (python)leetcode刷题笔记05 Longest Palindromic Substring

    5. Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You ...

  7. 【leetcode刷题笔记】Minimum Window Substring

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  8. 【leetcode刷题笔记】Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  9. LeetCode刷题笔记(1-9)

    LeetCode1-9 本文更多是作为一个习题笔记,没有太多讲解 1.两数之和 题目请点击链接 ↑ 最先想到暴力解法,直接双循环,但是这样复杂度为n平方 public int[] twoSum(int ...

随机推荐

  1. Eclipse更改默认工作环境编码为UTF-8(9.6)

    1 window---->preference------>General----->Workspace---->Text file encoding---->Other ...

  2. flume配置和说明(转)

    Flume是什么 收集.聚合事件流数据的分布式框架 通常用于log数据 采用ad-hoc方案,明显优点如下: 可靠的.可伸缩.可管理.可定制.高性能 声明式配置,可以动态更新配置 提供上下文路由功能 ...

  3. SpringSecurity学习四----------基于不同角色跳转到不同URL

    © 版权声明:本文为博主原创文章,转载请注明出处 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0& ...

  4. linux的用户、群组

    1.      用户及passwd文件 1)      掌握/etc/passwd文件的功能:存储所有用户的相关信息,该文件也被称为用户信息数据库(Database). 2)      /etc/pa ...

  5. MySQL中in(常量列表)的执行计划

    我们在写sql的时候,经常用到in,in后面跟一堆常量列表,如id.有人说in的效率很高,而有人说很低:有人说in能使用索引,还有人说in不能使用索引... 到底是一个怎样的情况呢?我们分析以下几种情 ...

  6. FILE 创建

    public class CreateDelFileUtils implements Serializable{ /** * */ private static final long serialVe ...

  7. IntelliJ IDEA 2014 付费版 免费版比较

    http://www.jetbrains.com/idea/features/editions_comparison_matrix.html Freemarker, Velocity IDE Feat ...

  8. js中return;return true return false 的区别

    return 定义: return 语句会 终止函数的执行 并 返回函数的值. 注意这两个: 1.终止函数的执行 2.返回函数的值 返回函数的值这里就不过多叙述了,就是 return 变量 先看下面的 ...

  9. IOS程序国际化

    1.1 新建一个Single View app模版项目,命名为Localization. 1.2 新建后,可以看到工作目录结构文件如下,单击InfoPlist.strings,查看右边的属性,在Loc ...

  10. 解决ubuntu无法进入unity模式

    终端输入如下命令: 1.sudo add-apt-repository ppa:gnome3-team/gnome3 2.sudo apt-get update 3.sudo apt-get inst ...