-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindSubstring.java
More file actions
66 lines (56 loc) · 2.12 KB
/
FindSubstring.java
File metadata and controls
66 lines (56 loc) · 2.12 KB
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
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class FindSubstring {
public static List<Integer> findSubstring(String S, String[] L) {
ArrayList<Integer> ret = new ArrayList<Integer>();
if(S==null|| L == null || L.length == 0) return ret;
int length = L[0].length();
if(S.length() < length*L.length) return ret;
HashMap<String, Integer> ls = new HashMap<String, Integer>();
for(String s:L) {
if(!ls.containsKey(s))
ls.put(s, 1);
else
ls.put(s, ls.get(s)+1);
}
System.out.println(ls);
HashMap<String, Integer> ss = new HashMap<String, Integer>();
for(int i=0; i<length; i++) {
int begin = i;
int count = 0;
ss.clear();
for(int j=i; j+length<=S.length(); j+=length) {
String temp = S.substring(j, j+length);
if(ls.containsKey(temp)) {
if(!ss.containsKey(temp)){ ss.put(temp, 1);
count++;
} else if(ss.get(temp)<ls.get(temp)){
ss.put(temp, ss.get(temp)+1);
count++;
} else {
String delete = null;
while(!((delete = S.substring(begin, begin+length)).equals(temp))) {
begin = begin + length;
ss.put(delete, ss.get(delete)-1);
count--;
}
begin = begin + length;
}
if(count == L.length)
ret.add(begin);
} else {
ss.clear();
count = 0;
begin = j + length;
}
}
}
return ret;
}
public static void main(String[] args) {
String S = "abaababbaba";
String[] L = {"ab","ba","ab","ba"};
System.out.println(FindSubstring.findSubstring(S, L));
}
}