-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSubstringwithConcatenationofAllWords.java
47 lines (43 loc) · 1.5 KB
/
SubstringwithConcatenationofAllWords.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
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by cpacm on 2017/5/16.
*/
public class SubstringwithConcatenationofAllWords {
public static void main(String[] args) {
System.out.println(findSubstring("aaaaaaaa", new String[]{"aa", "aa", "aa"}));
}
public static List<Integer> findSubstring(String s, String[] words) {
List<Integer> res = new ArrayList<>();
if (s == null || words == null || s.length() == 0 || words.length == 0) {
return res;
}
Map<String, Integer> map = new HashMap<>();
for (String str : words) {
map.put(str, map.containsKey(str) ? map.get(str) + 1 : 1);
}
int len = words[0].length();
for (int i = 0; i <= s.length() - len * words.length; i++) {
Map<String, Integer> tempMap = new HashMap<>(map);
int temp = i;
int count = 0;
String tempStr = s.substring(temp, temp + len);
while (tempMap.containsKey(tempStr) && tempMap.get(tempStr) > 0) {
tempMap.put(tempStr, tempMap.get(tempStr) - 1);
temp = temp + len;
count++;
if (temp + len <= s.length()) {
tempStr = s.substring(temp, temp + len);
} else {
break;
}
}
if (count == words.length) {
res.add(i);
}
}
return res;
}
}