-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathSubstringWithConcatOfAllWords.java
36 lines (36 loc) · 1.28 KB
/
SubstringWithConcatOfAllWords.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
public class Solution {
public ArrayList<Integer> findSubstring(String S, String[] L) {
// Start typing your Java solution below
// DO NOT write main() function
ArrayList<Integer> result = new ArrayList<Integer>();
HashMap<String, Integer> toFind = new HashMap<String, Integer>();
HashMap<String, Integer> found = new HashMap<String, Integer>();
int m = L.length, n = L[0].length();
for (int i = 0; i < m; i ++){
if (!toFind.containsKey(L[i])){
toFind.put(L[i], 1);
}
else{
toFind.put(L[i], toFind.get(L[i]) + 1);
}
}
for (int i = 0; i <= S.length() - n * m; i ++){
found.clear();
int j;
for (j = 0; j < m; j ++){
int k = i + j * n;
String stub = S.substring(k, k + n);
if (!toFind.containsKey(stub)) break;
if(!found.containsKey(stub)){
found.put(stub, 1);
}
else{
found.put(stub, found.get(stub) + 1);
}
if (found.get(stub) > toFind.get(stub)) break;
}
if (j == m) result.add(i);
}
return result;
}
}