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. [BZOJ2669][CQOI2012]局部极小值:DP+容斥原理

    分析 题目要求有且只有一些位置是局部极小值.有的限制很好处理,但是只有嘛,嗯...... 考虑子集反演(话说这个其实已经算是超集反演了吧还叫子集反演是不是有点不太合适),枚举题目给出位置集合的所有超集 ...

  2. 常用的vi快捷方式

    一般情况来说: 0代表行首,$代表行末 $,G代表最后一行 光标移动 0 移动到本行最前面 $ 移动到本行最后 G 移动文件最后一行 nG 移动到文件第n行 gg 移动到文件第一行 n[space]移 ...

  3. 步步向前之Element-UI

    Table 固定表头 只要在el-table元素中定义了height属性,即可实现固定表头的表格,而不需要额外的代码.例如: <el-table :data="tableData3&q ...

  4. Vue中 v-bind和v-on 缩写

    v-bind 缩写 <!-- 完整语法 --> <a v-bind:href="url">...</a> <!-- 缩写 --> & ...

  5. QbztDay1游记

    qbzt爆零记Day1 T1 我们为什么不把这个机器人直接报废掉呢? 素质题目 其实这就是个膜你模拟 坑点: 如果走到了水池并掉进去了,位置并不是水池前面的格子,而是执行这条指令之前的位置 来自老师的 ...

  6. SQLAlcvchem

    一.安装(稳定版的1.2.17) 二.一般使用(切记切记不要使用模块的名字作为项目名字,否则会出现玄学解决不了的问题------坑) #.导入SQLALchemy from sqlalchemy.ex ...

  7. ubuntu的无线网无法连上

    自己的笔记本可以连上wireless,但是实验室的台式机无法连上. 有无线显示,就是无法连上. 后来把连在机箱上的网线拔了以后可以连无线了.如果有网线连接,系统优先会选择有线的上网.

  8. bigdata数据分析(二):关闭防火墙&安装telnet

    先检查CentOS7.0是否已经安装以下两个安装包:telnet-server.xinetd.命令如下: rpm -qa telnet-server rpm -qa xinetd 如果没有安装,则先安 ...

  9. adb 性能测试(内存)

    内存测试: 1.使用数据线将手机与电脑连接: 2.手机打开待测APP,即打开进程: 3.打开cmd命令,获取设备列表:输入adb devices; 4.进入该设备的shell环境,输入:adb -s  ...

  10. Django 优秀资源大全

    版权: https://github.com/haiiiiiyun/awesome-django-cn 转自:https://www.jianshu.com/p/38c4dd6d8e28 Awesom ...