forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
56 lines (53 loc) · 2.18 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class Solution {
public List<Integer> findSubstring(String s, String[] words) {
List<Integer> re = new ArrayList<>();
if(s == null || words == null || words.length == 0 || words[0] == null) {
return re;
}
if(s.length() == 0 || words[0].length() == 0 || s.length() < words.length * words[0].length()) {
return re;
}
// 用< 单词,出现次数 > 来存储 words 中的元素,方便查找
HashMap<String,Integer> map = new HashMap();
for (String string : words) {
map.put(string, map.getOrDefault(string,0) + 1);
}
int len = words[0].length();
int strLen = s.length();
int lastStart = len * words.length - len;
for (int i = 0; i < len; i++) {
for (int j = i; j <= strLen - len - lastStart; j += len) {
String tempStr = s.substring(j, j + len);
if(map.containsKey(tempStr)) {
HashMap<String,Integer> searched = new HashMap<>();
// 从后向前依次对比
int tempIndex = j + lastStart;
String matchedStr = s.substring(tempIndex, tempIndex + len);
while (tempIndex >= j && map.containsKey(matchedStr)) {
// 正确匹配到单词
if(searched.getOrDefault(matchedStr,0) < map.get(matchedStr)) {
searched.put(matchedStr, searched.getOrDefault(matchedStr,0) + 1);
}
else {
break;
}
tempIndex -= len;
if(tempIndex < j) {
break;
}
matchedStr = s.substring(tempIndex, tempIndex + len);
}
// 完全匹配所以单词
if(j > tempIndex) {
re.add(j);
}
// 从tempIndex 到 tempIndex + len 这个单词不能正确匹配
else {
j = tempIndex;
}
}
}
}
return re;
}
}