forked from yuanx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubstringWithConcatenationOfAllWords.java
More file actions
52 lines (46 loc) · 1.26 KB
/
SubstringWithConcatenationOfAllWords.java
File metadata and controls
52 lines (46 loc) · 1.26 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
import java.util.*;
public class Solution {
public ArrayList<Integer> findSubstring(String S, String[] L) {
// Start typing your Java solution below
// DO NOT write main() function
Map<String, Integer> hash = new HashMap<String, Integer>();
for(String s : L){
if(hash.containsKey(s))
hash.put(s, hash.get(s)+1);
else
hash.put(s, 1);
}
ArrayList<Integer> res = new ArrayList<Integer>();
int m = L.length;
int wordlen = L[0].length();
for(int i=0; i<= S.length()-m*wordlen; i++){
String temp = S.substring(i, i+m*wordlen);
if(isString(temp, L, hash))
res.add(i);
}
return res;
}
public boolean isString(String t, String[] L, Map<String, Integer> hash){
Map<String, Integer> temphash = new HashMap<String, Integer>();
int len = L[0].length();
int i=0;
while(i<t.length()){
String temp = t.substring(i, i+len);
if(!hash.containsKey(temp))
return false;
else{
if(!temphash.containsKey(temp))
temphash.put(temp, 1);
else{
int n = temphash.get(temp);
if(n+1>hash.get(temp))
return false;
else
temphash.put(temp, n+1);
}
}
i+=len;
}
return true;
}
}