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. Android应用程序开发以及背后的设计思想深度剖析

    1 http://www.uml.org.cn/mobiledev/201211063.asp 2 ...

  2. 约瑟夫环用php实现

    百度百科的解释:约瑟夫环(约瑟夫问题)是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围.从编号为k的人开始报数,数到m的那个人出列:他的下一个人又从1开始报数,数 ...

  3. matplotlib表面三维图

    1.basic numpy.meshgrid 由一维数组到二维数组,用于生成网格数据 matplotlib python绘图库 2.code In [88]: from mpl_toolkits.mp ...

  4. 转MQTT--mosquitto服务器系统内容主题

    MQTT客户端可以通过订阅位于$SYS层次下的主题来查看mosquitto服务器的状态信息.标记为Static的主题对于每一次订阅只发布一次.其它所有主题每隔sys_interval(在mosquit ...

  5. MySQL 用户与权限管理

    MySQL权限系统的主要功能是证实连接到一台给定主机的用户,而且赋予该用户在数据库上的相关DML,DQL权限.MySQL存取控制包括2个阶段,一是server检查是否同意你连接:二是假定你能连接,se ...

  6. iOS tableView Section圆角方案

    给tableView的section设置圆角 首先给让cell左右偏移一点的距离,通过重写cell的setframe方法来实现 -(void)setFrame:(CGRect)frame{ CGFlo ...

  7. Duang,HUAWEI DevEco IDE全面升级啦

    想感受全新UI带来的视觉及交互体验. HiKey970开发板调测. HiAI API推荐和收藏. 深度AI模型分析等新功能, 体验高清晰度和流畅度的远程AI真机调测吗? 全新的UI设计 采用最优秀的视 ...

  8. 搭建apphouse(docker镜像私服)

    1.下载apphouse,地址为http://www.youruncloud.com/soft.html2.上传AppHouse_v1.0.2.tar到服务器并解压3../apphouse_insta ...

  9. lnmp 环境require(): open_basedir restriction in effect 错误

    最近配置开发用的lnmp环境,环境配置完成后,爆500错误,查看nginx错误日志 open_basedir 将 PHP 所能打开的文件限制在指定的目录树,包括文件本身 错误日志显示,访问脚本不在 o ...

  10. oracle 11g r2 blob类型getString报错问题

    摘要: 问题: 在hibernate中实体类中blob类型字段为 private String textBlob; 查询时报错: java.sql.SQLException: 无效的列类型: getS ...