【LeetCode】Substring with Concatenation of All Words

来源:互联网 发布:火焰切割圆弧怎么编程 编辑:程序博客网 时间:2024/05/15 23:50

Substring with Concatenation of All Words 

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

For example, given:
S"barfoothefoobarman"
L["foo", "bar"]

You should return the indices: [0,9].
(order does not matter).

【解题思路】
我看了好几天都没明白题目到底是什么意思,英文描述和汉语描述起来还是有很大差异。
事实上求解的是S中包含L中所有单词的index,换句话说,假设L中单词长度为wordLen,总长为wordLen*len,从index到距离为总长的这一段包含L中所有的单词。
例如题目中的0到5,9到14包含L中的所有单词。
有两种思路解决这个问题。
1、暴力搜索(Java AC的代码是1044ms)
针对S中每个字符开始搜索一次长度为wordLen*len的字符串,是否包含L中的所有单词。
这个很好统计,Map就可以直接搞定。思路很好想,同时也很费时。

Java AC

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 - len * wordLen + 1;for (int i = 0; i < max; i++) {Map<String, Integer> numMap = new HashMap<String, Integer>();int j = 0;for (; j < len; j++) {int start = i + j * wordLen;int end = start + wordLen;String tempStr = S.substring(start, end);if (!wordsMap.containsKey(tempStr)) {break;}int num = 1;if (numMap.get(tempStr) != null) {num += numMap.get(tempStr);}if (num > wordsMap.get(tempStr)) {break;}numMap.put(tempStr, num);}if (j == len) {list.add(i);}}return list;    }}
2、最小滑动窗口(Java AC的代码是448ms)

因为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
这样,针对每个分组,可以利用最小滑动窗口的思想,快速的判断是否包含需要的字符串。
直观上来看,1和2好像都是需要从每个字符开始搜索,实际上,2利用两个指针去在S中寻找满足条件的字符串,并且是每次+wordLen,而且不会重复的去统计,节省了很多时间。

Java AC

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);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;    }}


0 0
原创粉丝点击