[LeetCode][Java] 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 starting indices of substring(s) in s that
is a concatenation of each word in wordsexactly once and without any intervening characters.
For example, given:
s: "barfoothefoobarman"
words: ["foo", "bar"]
You should return the indices: [0,9]
.
(order does not matter).
题意:
给定一个字符串s,
和一组字符串数组words,数组中的全部字符串长度都一样。从字符串s中找出全部子串的開始标号。这些子串是words中全部字符串的组合。而且每一个字符串仅仅出现一次且没有其它字符插入他们之间,这些字符串的排列顺序无所谓。
算法分析:
* 由于L中全部单词的长度是一样的。这样依据wordLen。能够将S分为wordLen组,实际意思是这种。
* 以题目中barfoothefoobarman举例,L中单词长度为3。能够分为
* bar|foo|the|foo|bar|man
* ba|rfo|oth|efo|oba|rma|n
* b|arf|oot|hef|oob|arm|an
* 这样。针对每一个分组,能够利用最小滑动窗体的思想,高速的推断是否包括须要的字符串。
* 直观上来看,是须要从每一个字符開始搜索,实际上,利用两个指针去在S中寻找满足条件的字符串,并且是每次+wordLen。并且不会反复的去统 * 计。节省
* 了非常多时间。
方法二:
思路仍然是维护一个窗体。如果当前单词在字典中,则继续移动窗体右端。否则窗体左端能够跳到字符串下一个单词了。如果源字符串的长度为n。字典中单词的长度为l。由于不是一个字符。所以我们须要对源字符串全部长度为l的子串进行推断。
每次按顺序在源字符串中截取和字典中全部字符串长度相等的长度,推断新截取的子串和字典中字符串是否匹配,匹配就增加到结果中。不匹配就依次继续在源字符串中截取新的子串。反复上述过程直到结束。
AC代码:
<span style="font-size:12px;">public class Solution
{
public ArrayList<Integer> findSubstring(String S, String[] L)
{
ArrayList<Integer> list = new ArrayList<Integer>();
int len = L.length;
if (len == 0)
return list; int wordLen = L[0].length();
Map<String, Integer> wordsMap = new HashMap<String, Integer>();
for (int i = 0; i < len; i++)
{
int num = 1;
if (wordsMap.get(L[i]) != null)
num += wordsMap.get(L[i]);
wordsMap.put(L[i], num);
}
int slen = S.length();
int max = slen - wordLen + 1;
for (int i = 0; i < wordLen; i++)
{
Map<String, Integer> numMap = new HashMap<String, Integer>();
int count = 0;
int start = i;
for (int end = start; end < max; end += wordLen)
{
String tempStr = S.substring(end, end + wordLen);
if (!wordsMap.containsKey(tempStr))//给定字符串数组中不包括当前的字符串,直接跳到下一个字符串
{
numMap.clear();
count = 0;
start = end + wordLen;
continue;
} int num = 1;
if (numMap.containsKey(tempStr))
num += numMap.get(tempStr);
numMap.put(tempStr, num); if (num <= wordsMap.get(tempStr))
count++;//仅仅有在小于给定数组元素个数的情况下才自加
else
{
while (numMap.get(tempStr) > wordsMap.get(tempStr))
{
tempStr = S.substring(start, start + wordLen);//在如今的map尾部中出现大于给定数组元素个数的情况是时,去除map头部元素
numMap.put(tempStr, numMap.get(tempStr) - 1);
if (numMap.get(tempStr) < wordsMap.get(tempStr))
count--;//去除了元素了。个数自来就少了一个 start += wordLen;//相应的起始元素也往后移动了一个
}
}
if (count == len)
{
list.add(start);
tempStr = S.substring(start, start + wordLen);//满足条件后去除头个元素,也就是又一次后移一个位置,看看后面的满足条件不
numMap.put(tempStr, numMap.get(tempStr) - 1);
count--;
start += wordLen;
}
}
}
return list;
}
}</span>
方法二:
public class Solution
{
public List<Integer> findSubstring(String S, String[] L)
{
List<Integer> result=new ArrayList<Integer>();
if(L.length==0||S.length()==0) return result;
int wordlen=L[0].length();
//map中存放L
HashMap<String,Integer> map=new HashMap<String,Integer>();
for(int i=0;i<L.length;i++)
{
Integer value=map.get(L[i]);
if(value==null)
value=1;
else
value+=1;
map.put(L[i],value);
} for(int i=0;i+wordlen<=S.length();i++)
{
if(i + wordlen * L.length > S.length())
{
break;
}
if(map.containsKey(S.substring(i,i+wordlen)))
{
boolean b=checkString(S.substring(i,i+wordlen*L.length),new HashMap<String,Integer>(map),wordlen);
if(b==true)
result.add(i);
} }
return result;
} //检查字符串S是不是map中字符串的组合
public boolean checkString(String s,HashMap<String,Integer> map,int wordlen)
{
boolean flag=true;
int i=0;
while(s.length()>0)
{
String temp=s.substring(0,wordlen);
Integer value=map.get(temp);
if(value==null||value==0)
{
flag=false;
break;
}else{
value-=1;
map.put(temp,value);
s=s.substring(wordlen);//该子字符串从指定索引处的字符開始,直到此字符串末尾。
} }
return flag;
}
}
[LeetCode][Java] Substring with Concatenation of All Words的更多相关文章
- [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】Substring with Concatenation of All Words
Substring with Concatenation of All Words You are given a string, S, and a list of words, L, that ar ...
- LeetCode - 30. Substring with Concatenation of All Words
30. Substring with Concatenation of All Words Problem's Link --------------------------------------- ...
- LeetCode 030 Substring with Concatenation of All Words
题目要求:Substring with Concatenation of All Words You are given a string, S, and a list of words, L, th ...
- Java for LeetCode 030 Substring with Concatenation of All Words【HARD】
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 ...
- LeetCode 30 Substring with Concatenation of All Words(确定包含所有子串的起始下标)
题目链接: https://leetcode.com/problems/substring-with-concatenation-of-all-words/?tab=Description 在字符 ...
- [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 ...
随机推荐
- Python开发基础-Day17面向对象编程介绍、类和对象
面向对象变成介绍 面向过程编程 核心是过程(流水线式思维),过程即解决问题的步骤,面向过程的设计就好比精心设计好一条流水线,考虑周全什么时候处理什么东西.主要应用在一旦完成很少修改的地方,如linux ...
- 【BZOJ 3309】DZY Loves Math
http://www.lydsy.com/JudgeOnline/problem.php?id=3309 \[\sum_{T=1}^{min(a,b)}\sum_{d|T}f(d)\mu(\frac ...
- [Luogu1979][NOIP2013]华容道(BFS+SPFA)
考虑从起点到终点的过程,一定是先将空格子移到指定格子旁边,和指定格子交换,再移到下一个指定格子要到的地方,再交换,如此反复. 于是问题分为两个部分: 1.给定两个曼哈顿距离为2的格子求最短路,BFS即 ...
- CodeForces - 1000D Yet Another Problem On a Subsequence
题面在这里! 好智障的一个dp啊,一段开头的数字相当于下面要跟多少个数,直接滚动数组dp就行了... #include<bits/stdc++.h> #define ll long lon ...
- BZOJ 1827 [Usaco2010 Mar]gather 奶牛大集会(树形DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1827 [题目大意] 给出一棵有点权和边权的树, 请确定一个点,使得每个点到这个点的距离 ...
- 【计算几何】【预处理】【枚举】Urozero Autumn Training Camp 2016 Day 5: NWERC-2016 Problem K. Kiwi Trees
发现由于角的度数和边的长度有限制,那俩圆如果放得下的话,必然是塞在两个角里. 于是预处理n个圆心的位置(注意要判断那个圆会不会和其他的边界相交),然后n^2枚举俩角即可. #include<cs ...
- [NOIp2017提高组]列队
[NOIp2017提高组]列队 题目大意 一个\(n\times m(n,m\le3\times10^5)\)的方阵,每个格子里的人都有一个编号.初始时第\(i\)行第\(j\)列的编号为\((i-1 ...
- [NOI2017]整数
[NOI2017]整数 题目大意: \(n(n\le10^6)\)次操作维护一个长度为\(30n\)的二进制整数\(x\),支持以下两种操作: 将这个整数加上\(a\cdot2^b(|a|\le10^ ...
- hihocoder 1288 : Font Size (微软2016校园招聘4月在线笔试)
hihocoder 1288 笔试第一道..wa了好几次,也是无语..hihocoder错了不会告诉你失败的时候的测试集,这样有时候就很烦.. 遍历所有的字体,从min(w,h)开始逐渐变小开始遍历. ...
- tcpreplay工具使用
参考:http://www.cnblogs.com/jiayy/p/3447047.html 速率控制算法的大体思路就是,通过适当的sleep,增加包发送的时间,从而减小算出来的速率,以达到用户设 ...