-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindStartSubstringSolution.java
More file actions
56 lines (49 loc) · 1.61 KB
/
FindStartSubstringSolution.java
File metadata and controls
56 lines (49 loc) · 1.61 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
package findStartSubstring;
import java.util.ArrayList;
import java.util.List;
public class FindStartSubstringSolution {
public List<Integer> findSubstring(String s, String[] words) {
List<Integer> result = new ArrayList<Integer>();
int baseLength = words[0].length();
int length = words.length;
int[][] hash = new int[26][length];
int[] hashLength = new int[26];
for(int i = 0 ; i < 26; i++){
hashLength[i] = -1;
}
for(int i = 0; i < length; i++){
String tmp = words[i];
int b = (int)tmp.charAt(0) - 97;
hashLength[b]++;
hash[b][hashLength[b]] = i;
}
boolean flag = false;
for(int i = 0; i < s.length() - 3; i = i + baseLength){
String tmp = s.substring(i, i+3);
int letter = (int)tmp.charAt(0)-97;
int j = 0;
for(; j <= hashLength[letter]; j++){
if(tmp.equals(words[hash[letter][j]])){
if(!flag){
flag = true;
result.add(i);
}
break;
}
}
if(j > hashLength[letter]){
flag = false;
}
}
return result;
}
public static void main(String[] args){
String s = "barfoofoobarthefoobarman";
String[] words = new String[]{"bar","foo","the"};
FindStartSubstringSolution t = new FindStartSubstringSolution();
List<Integer> result = t.findSubstring(s, words);
for(int i = 0; i < result.size(); i++){
System.out.println(result.get(i));
}
}
}