You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.

Example 1:

Input:
s = "barfoothefoobarman",
words = ["foo","bar"]
Output: [0,9]
Explanation: Substrings starting at index 0 and 9 are "barfoor" and "foobar" respectively.
The output order does not matter, returning [9,0] is fine too.

Example 2:

Input:
s = "wordgoodgoodgoodbestword",
words = ["word","good","best","word"]
Output: []
 
class Solution {
public List<Integer> findSubstring(String s, String[] words) {
List<Integer> ret = new ArrayList<>();
if(words==null || words.length==0) return ret; int start; //start index of s
int cur; //current index of s
Map<String, Integer> wordCnt= new HashMap<String, Integer>(); //count of each word
Map<String, Integer> strCnt= new HashMap<String, Integer>(); //count of each word while traversing string
String w;
String w_to_del;
int startIndex;
int cnt;
int len = words[0].length();
int strLen = words.length * len; //initialize map
for(String str: words){
wordCnt.put(str,wordCnt.getOrDefault(str,0)+1);
} for(int j = 0; j < len; j++){ //word可能会出现在s的任意位置,而非仅仅0,len,2*len...的位置
startIndex = j;
strCnt.clear();
for(int i = j; i <= s.length()-len; i+=len){
w = s.substring(i,i+len);
if(wordCnt.containsKey(w)){ //word in words
strCnt.put(w, strCnt.getOrDefault(w,0)+1);
//number of word in string is more than that in words array, move right startIndex
while(strCnt.get(w) > wordCnt.get(w)){
w_to_del = s.substring(startIndex,startIndex+len);
strCnt.put(w_to_del,strCnt.get(w_to_del)-1);
startIndex += len;
} //find a match
if(i + len - startIndex == strLen){
ret.add(startIndex); //start to find another match from startIndex+len
w_to_del = s.substring(startIndex,startIndex+len);
strCnt.put(w_to_del,strCnt.get(w_to_del)-1);
startIndex += len;
} }
else{ //word not in words
startIndex = i+len;
strCnt.clear();
}
}
}
return ret;
}
}

时间复杂度:通过window的方式,在每个word的起始位置,只要遍历一遍s,就能完成查询。一共有size =words.length个起始位置,所以时间复杂度是O(n*size),其中n为s的长度,在s很长的时候,可以认为size是可忽略的常量,时间复杂度为O(n)。

30. Substring with Concatenation of All Words (JAVA)的更多相关文章

  1. LeetCode - 30. Substring with Concatenation of All Words

    30. Substring with Concatenation of All Words Problem's Link --------------------------------------- ...

  2. [Leetcode][Python]30: Substring with Concatenation of All Words

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 30: Substring with Concatenation of All ...

  3. [LeetCode] 30. Substring with Concatenation of All Words 解题思路 - Java

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

  4. leetCode 30.Substring with Concatenation of All Words (words中全部子串相连) 解题思路和方法

    Substring with Concatenation of All Words You are given a string, s, and a list of words, words, tha ...

  5. LeetCode HashTable 30 Substring with Concatenation of All Words

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

  6. Java [leetcode 30]Substring with Concatenation of All Words

    题目描述: You are given a string, s, and a list of words, words, that are all of the same length. Find a ...

  7. 30. Substring with Concatenation of All Words

    题目: You are given a string, s, and a list of words, words, that are all of the same length. Find all ...

  8. [LeetCode] 30. Substring with Concatenation of All Words 串联所有单词的子串

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

  9. 【LeetCode】30. Substring with Concatenation of All Words

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

随机推荐

  1. python-opencv中的cv2.inRange函数

    本次目标是将一副图像从rgb颜色空间转换到hsv颜色空间,颜色去除白色背景部分 具体就调用了cv2的两个函数,一个是rgb转hsv的函数 具体用法 hsv = cv2.cvtColor(rgb_ima ...

  2. js控制手机保持亮屏的库,解决h5移动端,自动息屏问题

    一些说明:我用Laya(ts)开发小游戏,有需要保持手机屏幕常亮的需求(非必须的),然后作为小白的我就在网上找到了这个库,大概了解下,应该是通过播放空视频的原理来保持手机屏幕常亮,然后就放到项目中试了 ...

  3. 哈夫曼(Huffman)树及其应用

    Huffman树又称最优树,是一类带权路径长度最短的树,带权路径长度为从该节点到树根之间的路径长度与节点上权值的成积. 那么如何构建一个Huffman树呢?就需要Huffman算法 1.利用给定的n个 ...

  4. SQL语句多个字段排序

    写在前面的话: 本次迭代,分配的任务有点多啊,好在本妈妈朝10晚6的加班加点.其中一项就是列表展示啊, 展示就展示呗,还要排序.排序也可以,测试时排序字段值为啥都一样啊.结果点一下一个顺序. 就想在第 ...

  5. PADS LAYOUT的一般流程

    1.概述    本文档的目的在于说明使用PADS的印制板设计软件PowerPCB进行印制板设计的流程和一些注意事项,为一个工作组的设计人员提供设计规 范,方便设计人员之间进行交流和相互检查. 2.设计 ...

  6. python - Tkinter 模块 - python 自带的gui模块

    Tkinter模块("Tk 接口")是Python的标准Tk GUI工具包的接口,位Python的内置模块,直接import tkinter即可使用. 1.创建窗口 from Tk ...

  7. 求旋转数组的最小数字(剑指offer)

    把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数 ...

  8. Webpack的tapable 为什么要使用 new Funtion 来生成静态代码

    为了保持代码的单态(monomorphism). 这涉及到了js引擎优化的一些问题, tapable从1.0.0版本开始就用new Function来生成静态代码最后来来执行, 以确保得到最优执行效率 ...

  9. 3D聚类

    1 3D聚类和普通的二维聚类实质一样,只不过维数太高了,用三维图来表示了. 下面将官网的改成只生成一个图了 #!/usr/bin/python # -*- coding:utf-8 -*- print ...

  10. fixture之autouse=True

    平常写自动化用例会写一些前置的fixture操作,用例需要用到就直接传该函数的参数名称就行了.当用例很多的时候,每次都传这个参数,会比较麻烦.fixture里面有个参数autouse,默认是Fasle ...