1. 题目描述:
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 = “wordgoodstudentgoodword”,
words = [“word”,“student”]
Output: []
2. 思路分析:
题目的意思是给定一个字符串和一个包含若干个词的列表,然后找出列表中所有词的各种组合在字符串中的位置。
由于数组中所有单词的长度都是一样的,我们可以像3.Longest Substring with At Most Two Distinct Characters
中一样,把每个词当作一个字母来看待,但是要遍历K次,K是单词的长度,因为我们要分别统计从下标0开头,从下标1开头。。。直到下标K-1开头的字符串。举例来说foobarfoo,给定数组是[foo, bar],那我们要对foo|bar|foo搜索一次,对oob|arf|oo搜索一次,对oba|rfo|o搜索一次,我们不用再对bar|foo搜索,因为其已经包含在第一种里面了。每次搜索中,我们通过哈希表维护一个窗口,比如foo|bar|foo中,我们先拿出foo。如果foo都不在数组中,那说明根本不能拼进去,则哈希表全部清零,从下一个词开始重新匹配。但是foo是在数组中的,所以给当前搜索的哈希表计数器加上1,如果发现当前搜索中foo出现的次数已经比给定数组中foo出现的次数多了,我们就要把上一次出现foo之前的所有词都从窗口中去掉,如果没有更多,则看下一个词bar,不过在这之前,我们还要看看窗口中有多少个词了,如果词的个数等于数组中词的个数,说明我们找到了一个结果。
3. Java代码:
源代码
:见我GiHub主页
代码:
public static List<Integer> findSubstring(String s, String[] words) {
List<Integer> result = new ArrayList<>();
if (s == null || s.length() == 0 || words == null || words.length == 0) {
return result;
}
// 用于记录词表中每个词的数目
Map<String, Integer> wordCount = new HashMap<>();
for (String word : words) {
int cnt = wordCount.getOrDefault(word, 0);
wordCount.put(word, cnt + 1);
}
// 用于记录每个词的长度,所有词长度一样
int len = words[0].length();
for (int i = 0; i < len; i++) {
// 用于记录当前已匹配到词表中的各个词的个数, 当作一个滑动窗口的作用
Map<String, Integer> curWordCount = new HashMap<>();
// 记录移动窗口的起始位置
int start = i;
// 记录已匹配词的个数
int count = 0;
for (int j = i; j <= s.length() - len; j += len) {
String curWord = s.substring(j, j + len);
// 表示当前词不在词表中,清空窗口,重新开始
if (!wordCount.containsKey(curWord)) {
curWordCount.clear();
count = 0;
start = j + len;
continue;
}
int curCnt = curWordCount.getOrDefault(curWord, 0);
curWordCount.put(curWord, curCnt + 1);
count++;
// 如果当前的词多匹配了,此时需要不断将最左边的词清理出去,直到当前词的数目满足条件
while (curWordCount.get(curWord) > wordCount.get(curWord)) {
String leftWord = s.substring(start, start + len);
curWordCount.put(leftWord, curWordCount.get(leftWord) - 1);
count--;
start += len;
}
// 表示一次匹配成功,存入结果中
if (count == words.length) {
result.add(start);
String lefWord = s.substring(start, start + len);
curWordCount.put(lefWord, curWordCount.get(lefWord) - 1);
count--;
start += len;
}
}
}
return result;
}