/
Solution.java
executable file
·67 lines (41 loc) · 1.43 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
public class Solution {
public List<Integer> findSubstring(String s, String[] words) {
}
}
public class Solution {
boolean checkCat(String s, HashMap<String, Integer> lm, int wordLen){
while(s.length() > 0){
String w = s.substring(0, wordLen);
Integer v = lm.get(w);
if(v == null) return false;
if(v == 0) return false;
s = s.substring(wordLen);
v--;
lm.put(w, v);
}
return true;
}
public List<Integer> findSubstring(String S, String[] L) {
if(L.length == 0) return new ArrayList<Integer>();
ArrayList<Integer> rt = new ArrayList<Integer>();
HashMap<String, Integer> lm = new HashMap<String, Integer>();
for(String l : L){
Integer v = lm.get(l);
if(v == null) v = 0;
v++;
lm.put(l, v);
}
final int wordLen = L[0].length();
for(int i = 0; i + wordLen <= S.length(); i++){
if(lm.containsKey(S.substring(i, i + wordLen))){
if(i + wordLen * L.length > S.length()){
break;
}
if(checkCat(S.substring(i, i + wordLen * L.length), new HashMap<String, Integer>(lm), wordLen)){
rt.add(i);
}
}
}
return rt;
}
}