-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubstringwithConcatenationofAllWords.java
More file actions
102 lines (81 loc) · 2.05 KB
/
SubstringwithConcatenationofAllWords.java
File metadata and controls
102 lines (81 loc) · 2.05 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
;
public class SubstringwithConcatenationofAllWords {
public static void main(String[] args) {
List<Integer> integers = findSubstring("sheateateseatea", new String[] {
"sea", "tea", "ate" });
for (Integer i : integers) {
System.out.println(i);
}
}
//Leetcode Judge Accepted
public static List<Integer> findSubstring(String S, String[] L) {
List<Integer> returnList = new ArrayList<Integer>();
if (L == null || L.length == 0 || S == null || S.length() == 0
|| S.length() < L[0].length()) {
return returnList;
}
int wordLen = L[0].length();
HashMap<String, Integer> map = new HashMap<String, Integer>();
for (String s : L) {
if (map.containsKey(s)) {
map.put(s, map.get(s) + 1);
} else {
map.put(s, 1);
}
}
int numOfL = L.length;
for (int initPos = 0; initPos < wordLen; initPos++) {
int i = initPos, j = initPos;
while (true) {
if (numOfL == 0) {
returnList.add(i);
String preOne = S.substring(i, i + wordLen);
map.put(preOne, map.get(preOne) + 1);
i += wordLen;
numOfL++;
continue;
}
if (j >= S.length()) {
while (i < j) {
String preOne = S.substring(i, i + wordLen);
map.put(preOne, map.get(preOne) + 1);
i += wordLen;
numOfL++;
}
break;
}
String curOne = S.substring(j,
Math.min(S.length(), j + wordLen));
if (map.containsKey(curOne)) {
if (map.get(curOne).intValue() == 0) {
while (i < j) {
String preOne = S.substring(i, i + wordLen);
map.put(preOne, map.get(preOne) + 1);
i += wordLen;
numOfL++;
if (curOne.equals(preOne))
break;
}
} else {
map.put(curOne, map.get(curOne) - 1);
j += wordLen;
numOfL--;
}
} else {
while (i < j) {
String preOne = S.substring(i, i + wordLen);
map.put(preOne, map.get(preOne) + 1);
i += wordLen;
numOfL++;
}
i += wordLen;
j = i;
}
}
}
return returnList;
}
}