forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
38 lines (38 loc) · 1.25 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
class Solution {
public List<Integer> findSubstring(String s, String[] words) {
Map<String, Integer> cnt = new HashMap<>();
for (String w : words) {
cnt.put(w, cnt.getOrDefault(w, 0) + 1);
}
int subLen = words[0].length();
int n = s.length(), m = words.length;
List<Integer> ans = new ArrayList<>();
for (int i = 0; i < subLen; ++i) {
Map<String, Integer> cnt1 = new HashMap<>();
int l = i, r = i;
int t = 0;
while (r + subLen <= n) {
String w = s.substring(r, r + subLen);
r += subLen;
if (!cnt.containsKey(w)) {
l = r;
cnt1.clear();
t = 0;
continue;
}
cnt1.put(w, cnt1.getOrDefault(w, 0) + 1);
++t;
while (cnt1.get(w) > cnt.get(w)) {
String remove = s.substring(l, l + subLen);
l += subLen;
cnt1.put(remove, cnt1.get(remove) - 1);
--t;
}
if (m == t) {
ans.add(l);
}
}
}
return ans;
}
}