forked from yubinbai/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
51 lines (47 loc) · 1.74 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
import java.util.*;
public class Solution {
public List<Integer> findSubstring(String s, String[] L) {
ArrayList<Integer> ret = new ArrayList<Integer>();
int n = L.length;
int w = L[0].length();
for (int pos = 0; pos <= s.length() - w * n; pos++) {
HashMap<String, Integer> strCount = new HashMap<String, Integer>();
for (String str : L) {
Integer currCount = strCount.get(str);
strCount.put(str, currCount == null ? 1 : currCount + 1);
}
int step = 0;
for (; step < n; step++) {
String sub = s.substring(pos + w * step, pos + w + w * step);
Integer currCount = strCount.get(sub);
if (currCount != null && currCount > 0) {
strCount.put(sub, currCount - 1);
} else {
break;
}
}
if (step == n) ret.add(pos);
}
return ret;
}
public static void main(String[] args) {
Solution sol = new Solution();
String s;
String[] l;
s = "barfoothefoobarman";
l = new String[] {"foo", "bar"};
System.out.println(sol.findSubstring(s, l));
s = "lingmindraboofooowingdingbarrwingmonkeypoundcake";
l = new String[] {"fooo", "barr", "wing", "ding", "wing"};
System.out.println(sol.findSubstring(s, l));
s = "a";
l = new String[] {"a"};
System.out.println(sol.findSubstring(s, l));
s = "aaa";
l = new String[] {"a", "a"};
System.out.println(sol.findSubstring(s, l));
s = "mississippi";
l = new String[] {"is"};
System.out.println(sol.findSubstring(s, l));
}
}