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 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)的更多相关文章
- LeetCode - 30. Substring with Concatenation of All Words
30. Substring with Concatenation of All Words Problem's Link --------------------------------------- ...
- [Leetcode][Python]30: Substring with Concatenation of All Words
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 30: Substring with Concatenation of All ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- [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 ...
- 【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 ...
随机推荐
- springboot(四).配置FastJson自定义消息转化器
配置FastJson自定义消息转化器 一.fastJson简介 fastJson是阿里巴巴旗下的一个开源项目之一,顾名思义它专门用来做快速操作Json的序列化与反序列化的组件.它是目前json解析最快 ...
- [BZOJ2560]串珠子:状压DP+容斥原理
分析 为什么我去年6月做过这道题啊,估计当时抄的题解. 具体做法就是令\(f[S]\)表示保证连通点集\(S\)的方案数,\(g[S]\)表示不保证连通点集\(S\)的方案数. 容易想到: \[g[S ...
- Mongodb分片副本集集群搭建
一.环境准备 1.1.主机信息(机器配置要求见硬件及开发标准规范文档V1.0) 序号 主机名 IP 1 DB_01 10.202.105.52 2 DB_02 10.202.105.53 3 DB_0 ...
- [CSP-S模拟测试]:军训队列(DP+乱搞)
题目描述 有$n$名学生参加军训,军训的一大重要内容就是走队列,而一个队列的不规整程度是该队中最高的学生的身高与最矮的学生的身高差值的平方.现在要将$n$名参加军训的学生重新分成$k$个队列,每个队列 ...
- C/C++题库
1.下面的代码输出什么?为什么? void foo(void) { unsigned int a = 6; int b = -20; (a+b > 6)?puts(“>6”):puts(“ ...
- WAMP搭建与配置
使用WampServer整合软件包进行WAMP环境搭建 WampServer是一款由法国人开发的Apache Web服务器.PHP解释器以及MySQL数据库的整合软件包.免去了开发人员将时间花费在繁琐 ...
- c++实验10 图的应用实验
大体与上次实验相同,特点为图是邻接表存储结构 --博客后半部分有程序的所有代码-- 1.图邻接表存储结构表示及基本操作算法实现 所加载的库函数或常量定义及类的定义: #include "Se ...
- lgb模板
一 回归 1 提取训练集和测试集 2 制作标签,并检查标签是否有异常值 2 划分数据 https://www.jb51.net/article/152574.htm 3 建立model,写评价函数 h ...
- fiddler之编辑请求(composer)-发包
在需要针对接口进行发包操作时,可以使用composer标签,去编辑请求内容,进行请求. 界面显示如下: 1.Parsed 在该分页中,选择请求方法.设置请求地址和协议版本,上部分为请求的头信息.下半部 ...
- 打开Excel提示内存不足
来越南出差第一天,有个越南妹子跟我反应说Excel打不开,提示的是很常见的“内存不足”的错误, 这种问题一般的判断就是打开的程序太多了,关掉一些就可以了,重启都没解决, 在网上找了下,腾讯管家的这篇h ...