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. iOS开发之状态栏隐藏(问题篇)

    一.基本应用 相信基本的隐藏办法网上很多,这里只简单说明一下 1⃣️改变全局状态栏 1.在项目的Info.plist文件里设置UIViewControllerBasedStatusBarAppeara ...

  2. GB28181出内网

    最近关注GB28181的朋友很多,昨天有位朋友问到GB28181出内网的问题,希望我花5分钟的时间 讲讲如何通过GB28181协议将内网的摄像机视频推送到公网.要说清楚这个问题,5分钟的时间应该不 够 ...

  3. RAD Studio XE8 技术研讨会讲义与范例程序下载

     感谢各位程序猿亲临现场參加我们的公布会,现奉上会议当天的讲义与范例程序供大家參考: 2015/5/25~27北京.深圳 『RAD Studio XE8技术研讨会』 下载讲义:http://pan ...

  4. c# 获取毫秒值,时间戳

    获取时间戳(秒) (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000 获取时间戳(毫秒) (DateTime. ...

  5. Mac下面的SecureCRT(附破解方案) 更新到最新的7.3.2(转)

    转自 http://bbs.weiphone.com/read-htm-tid-6939481.html 继续更新到7.3.2的破解.只是升级了下secureCRT到7.3.2,方法还是不变 相信很多 ...

  6. spark 的一些常用函数 filter,map,flatMap,lookup ,reduce,groupByKey

    定义不带参数也不带返回值的函数(def :定义函数的关键字  printz:方法名称) scala> def printz = print("scala hello")   ...

  7. Foundation框架 - NSDictionary类、NSMutableDictionary类

    NSArray.NSSet.NSDictionary /* 集合 1.NSArray\NSMutableArray * 有序 * 高速创建(不可变):@[obj1, obj2, obj3] * 高速訪 ...

  8. 基于树莓派3B+Python3.5的OpenCV3.4的配置教程

    https://www.cnblogs.com/Pyrokine/p/8921285.html

  9. Android中应用安装分析

    #1 安装方式 1 安装系统APK和预制APK时,通过PMS的构造函数中安装,即第一次开机时安装应用,没有安装界面. 2 网络下载安装,通过应用商店等,即调用PackageManager.instal ...

  10. 批处理--md5校检

    @echo off rem 获取文件xx.zip的MD5 for /f "delims=" %%i in ('md5.exe xx.zip') do (set md5_var=%% ...