Skip to content

Commit 88518a6

Browse files
committed
Runtime: 68 ms (Top 73.46%) | Memory: 43.2 MB (Top 88.65%)
1 parent 5a28ee0 commit 88518a6

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
// Runtime: 68 ms (Top 73.46%) | Memory: 43.2 MB (Top 88.65%)
12
class Solution {
23
public List<Integer> findSubstring(String s, String[] words) {
3-
4+
45
HashMap<String, Integer> input = new HashMap<>();
56
int ID = 1;
67
HashMap<Integer, Integer> count = new HashMap<>();
@@ -9,17 +10,17 @@ public List<Integer> findSubstring(String s, String[] words) {
910
input.put(word, ID++);
1011
int id = input.get(word);
1112
count.put(id,count.getOrDefault(id,0)+1);
12-
13+
1314
}
1415
int len = s.length();
1516
int wordLen = words[0].length();
1617
int numWords = words.length;
1718
int windowLen = wordLen*numWords;
1819
int lastIndex = s.length()-windowLen;
19-
20+
2021
int curWordId[] = new int[len];
2122
String cur = " "+s.substring(0,wordLen-1);
22-
23+
2324
//Change to int array
2425
for(int i = 0; i< (len-wordLen+1); i++) {
2526
cur = cur.substring(1, cur.length())+s.charAt(i+wordLen-1);
@@ -30,32 +31,31 @@ public List<Integer> findSubstring(String s, String[] words) {
3031
}
3132
}
3233
List<Integer> res = new ArrayList<>();
33-
34+
3435
//compare using int make it faster 30 times in each comparison
3536
for(int i = 0; i<= lastIndex; i++) {
36-
37+
3738
HashMap<Integer, Integer> winMap = new HashMap<>();
3839
for(int j = 0; j < windowLen && curWordId[i] != -1; j+=wordLen) {
39-
40+
4041
int candidate = curWordId[j+i];
41-
42+
4243
if(!count.containsKey(candidate))
4344
break;
4445
else{
4546
winMap.put(candidate, winMap.getOrDefault(candidate, 0)+1);
4647
}
4748
if(winMap.get(candidate) > count.get(candidate))
4849
break;
49-
50-
50+
5151
if(j == (windowLen - wordLen) && winMap.size() == count.size()){
5252
res.add(i);
53-
53+
5454
}
55-
55+
5656
}
5757
}
58-
58+
5959
return res;
6060
}
61-
}
61+
}

0 commit comments

Comments
 (0)