-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem30.java
74 lines (63 loc) · 2.07 KB
/
Problem30.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import java.util.*;
class Problem30 {
public static void main(String[] args) {
String[] words = new String[]{"foo", "bar"};
String s = "barfoothefoobarman";
System.out.print(new Problem30().findSubstring(s, words));
}
public List<Integer> findSubstring(String s, String[] words) {
// 将words全排列,找到所有和新排列匹配的对象,时间复杂度为K!
// request length of total words as substring, and compare with s.
List<Integer> res = new ArrayList<>();
// edge
if (s.length() <= 0 || words.length <= 0 || words[0].length() <= 0) {
return res;
}
// slide model
int size = words.length * words[0].length(); // minimal pattern length;
int len = words[0].length();
//
HashMap<String, Integer> hashMap = new HashMap<>();
for (String word : words) {
if (hashMap.containsKey(word)) hashMap.put(word, hashMap.get(word) + 1);
else hashMap.put(word, 1);
}
int tmp = -1;
for (int pos = 0; pos < s.length() - size + 1; pos++) {
String tStr = s.substring(pos, len+pos);
if (hashMap.containsKey(tStr)) {
int t = hashMap.get(tStr)-1;
hashMap.put(tStr, t);
} else if (checkMap(hashMap)) {
pos--;
res.add(pos+len-size);
resetMap(hashMap);
}
}
return res;
}
boolean checkMap(HashMap<String, Integer> hashMap) {
// check if all value is 0
boolean flag = true;
Iterator iter = hashMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String, Integer> entry = (Map.Entry) iter.next();
String key = entry.getKey();
Integer val = entry.getValue();
if (val != 0) {
flag = false;
return flag;
}
}
return flag;
}
void resetMap(HashMap<String, Integer> hashMap) {
// reset all value to 1
Iterator iter = hashMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String, Integer> entry = (Map.Entry) iter.next();
String key = entry.getKey();
hashMap.put(key, 1);
}
}
}