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. (四)mysql -- 常用函数

    今天get一个,先记录一下 以后慢慢补充~ 将varchar转换成int 例如:select * from tb_1 order by cast(sport_sum as unsigned integ ...

  2. centos网卡配置NM_CONTROLLED=”yes” 慎用

    今天有1台服务器修改了 /etc/sysconfig/network-scripts/ifcfg-eth0 配置文件中的一个参数: NM_CONTROLLED=“yes” 修改成 NM_CONTROL ...

  3. sqli-labs(23)

    基于get的过滤了的注入 0X1测试闭合 /?id=' http://127.0.0.1/sql1/Less-23/?id=1%27%27 0X02 然后就是组合拳的操作了 未报错 那么应该是’闭合 ...

  4. js中的 for, for in, for of foreach,filter使用

    下面是对数组进行循环 var array = [ { id: , name: 'ohzri', birth: '1999.09.09', city: '湖北', salary: }, { id: , ...

  5. from sklearn import datasets运行错误:ImportError: DLL load failed: 找不到指定的程序------解决办法

    在运行集成学习的多数投票分类代码时,出现错误 from sklearn import datasets from sklearn.model_selection import cross_val_sc ...

  6. jvm内存模型学习心得

    昨天面试了两家,备受打击,问的多的就是jvm内存,然额真的是一头雾水.工作中用到的真是少之又少,面试还得问道, 今天恶补了下,在此作以下总结: jvm分为5部分 1.程序计数器 jvm支持多线程运行, ...

  7. preventDefault 和 stopPropagation

    概述 以前开发项目的时候,总是分不清楚 preventDefault 和 stopPropagation,每次都是用 @click.stop试一下,不能就用@click.prevent试一下.今天来好 ...

  8. Tensorflow 教程系列 | 莫烦Python

    Tensorflow 简介 1.1 科普: 人工神经网络 VS 生物神经网络 1.2 什么是神经网络 (Neural Network) 1.3 神经网络 梯度下降 1.4 科普: 神经网络的黑盒不黑 ...

  9. pg和mysql对比

    作者:方圆链接:https://www.zhihu.com/question/20010554/answer/15863274来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出 ...

  10. centos7:Kafka集群安装

    解压文件到安装目录 tar -zxvf kafka_2.10-0.10.2.1.tgz 1.进入目录 cd kafka_2.10-0.10.2.1 mkdir logs cd config cp se ...