forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubstring with Concatenation of All Words.java
61 lines (49 loc) · 1.9 KB
/
Substring with Concatenation of All Words.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
// Runtime: 68 ms (Top 73.46%) | Memory: 43.2 MB (Top 88.65%)
class Solution {
public List<Integer> findSubstring(String s, String[] words) {
HashMap<String, Integer> input = new HashMap<>();
int ID = 1;
HashMap<Integer, Integer> count = new HashMap<>();
for(String word: words) {
if(!input.containsKey(word))
input.put(word, ID++);
int id = input.get(word);
count.put(id,count.getOrDefault(id,0)+1);
}
int len = s.length();
int wordLen = words[0].length();
int numWords = words.length;
int windowLen = wordLen*numWords;
int lastIndex = s.length()-windowLen;
int curWordId[] = new int[len];
String cur = " "+s.substring(0,wordLen-1);
//Change to int array
for(int i = 0; i< (len-wordLen+1); i++) {
cur = cur.substring(1, cur.length())+s.charAt(i+wordLen-1);
if(input.containsKey(cur)){
curWordId[i] = input.get(cur);
} else {
curWordId[i] = -1;
}
}
List<Integer> res = new ArrayList<>();
//compare using int make it faster 30 times in each comparison
for(int i = 0; i<= lastIndex; i++) {
HashMap<Integer, Integer> winMap = new HashMap<>();
for(int j = 0; j < windowLen && curWordId[i] != -1; j+=wordLen) {
int candidate = curWordId[j+i];
if(!count.containsKey(candidate))
break;
else{
winMap.put(candidate, winMap.getOrDefault(candidate, 0)+1);
}
if(winMap.get(candidate) > count.get(candidate))
break;
if(j == (windowLen - wordLen) && winMap.size() == count.size()){
res.add(i);
}
}
}
return res;
}
}