forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubstringWithConcatenationOfAllWords.java
59 lines (44 loc) · 1.43 KB
/
SubstringWithConcatenationOfAllWords.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
57
58
59
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
public class SubstringWithConcatenationOfAllWords {
// 118ms,最直接的做法
// 这些words可能有重复
public List<Integer> findSubstring(String s, String[] words) {
List<Integer> list = new LinkedList<>();
if (words.length == 0 || s.length() == 0) {
return list;
}
int len = words[0].length();
int total = len * words.length;
if (s.length() < total) {
return list;
}
HashMap<String, Integer> map = new HashMap<>();
for (String word : words) {
map.put(word, 1 + map.getOrDefault(word, 0));
}
for (int i = 0; i <= s.length() - total; ) {
if (isMatch(s, i, i + total, len, map)) {
list.add(i);
}
i++;
}
return list;
}
private boolean isMatch(String s, int start, int end, int len, HashMap<String, Integer> map0) {
HashMap<String, Integer> map = new HashMap<>(map0);
for (int i = start; i < end; i += len) {
String t = s.substring(i, i + len);
int m = map.getOrDefault(t, 0);
if (m > 1) {
map.put(t, m - 1);
} else if (m == 1) {
map.remove(t);
} else {
return false;
}
}
return map.isEmpty();
}
}