-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubstringwithConcatenationofAllWords.java
More file actions
170 lines (156 loc) · 5.58 KB
/
SubstringwithConcatenationofAllWords.java
File metadata and controls
170 lines (156 loc) · 5.58 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package hard;
import static util.Utils.printArray;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
*
* ClassName: SubstringwithConcatenationofAllWords
* @author chenyiAlone
* Create Time: 2018/12/21 12:42:55
* Description: No.30
* 总结:
* 1. 使用了 Map 来进行计数和判定是否存在于数组中
* 2. 题目中的所有 word 的长度相等,简化了题目难度
*
* You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.
Example 1:
Input:
s = "barfoothefoobarman",
words = ["foo","bar"]
Output: [0,9]
Explanation: Substrings starting at index 0 and 9 are "barfoor" and "foobar" respectively.
The output order does not matter, returning [9,0] is fine too.
Example 2:
Input:
s = "wordgoodgoodgoodbestword",
words = ["word","good","best","word"]
Output: []
*=======================================
* 第一次尝试:采用的是暴力搜索,结果直接超时
*/
public class SubstringwithConcatenationofAllWords {
public List<Integer> findSubstring(String s, String[] words) {
List<Integer> res = new ArrayList<>();
if (s == null || words.length < 0) return res;
HashMap<String, Integer> map = new HashMap<>();
for (String word : words) {
map.put(word, map.getOrDefault(word, 0) + 1);
}
int n = words.length;
if (n == 0) return res;
int m = words[0].length();
for (int i = 0; i <= s.length() - m * n; i++) {
HashMap<String, Integer> copy = new HashMap<>(map);
int k = n;
int j = i;
while (k > 0) {
String str = s.substring(j, j + m);
if (!map.containsKey(str) || copy.get(str) < 1) break;
copy.put(str, copy.get(str) - 1);
k--;
j += m;
}
if (k == 0) res.add(i);
}
return res;
}
public static void main(String[] args) {
String s = "wordgoodgoodgoodbestword";
String[] words = {"word","good","best","good"};
System.out.println(new SubstringwithConcatenationofAllWords().findSubstring(s, words));
}
public List<Integer> findSubstring_Low(String s, String[] words) {
List<Integer> ans = new ArrayList<>();
Set<String> set = new HashSet<>();
permutaion(words, set, 0);
System.out.println("set.size() = " + set.size());
System.out.println(set);
// String str = "aaaaaa";
// kmpSearch(s, str, ans);
for (String str : set) {
System.out.println(s + " + " + str);
// kmpSearch(s, str, ans);
violenSearch(s, str, ans);
}
return ans;
}
public static void violenSearch(String str, String pattern, List<Integer> list) {
for (int i = 0; i <= str.length() - pattern.length(); i++) {
boolean isPat = true;
for (int j = 0; j < pattern.length(); j++) {
if (str.charAt(i + j) != pattern.charAt(j)) {
isPat = false;
break;
}
}
if (isPat) list.add(i);
}
}
public static void permutaion(String[] words, Set<String> set, int p) {
if (p == words.length) {
StringBuilder sb = new StringBuilder();
for (String s : words) {
sb.append(s);
// System.out.print(s + " ");
}
set.add(sb.toString());
}
for (int i = p; i < words.length; i++) {
String temp = words[p];
words[p] = words[i];
words[i] = temp;
permutaion(words, set, p + 1);
temp = words[p];
words[p] = words[i];
words[i] = temp;
}
}
public static int[] kmpArray(String needle) {
int[] nums = new int[needle.length() + 1];
nums[0] = -1;
nums[1] = 0;
int len = 0;
int i = 1, n = needle.length();
while (i < n) {
if (needle.charAt(len) == needle.charAt(i)) {
len++;
nums[i + 1] = len;
i++;
} else {
if (len > 0) {
len = nums[len];
} else {
nums[i + 1] = len;
i++;
}
}
}
return nums;
}
public static void kmpSearch(String haystack, String needle, List<Integer> list) {
int kmp[] = kmpArray(needle);
printArray(kmp);
int i = 0, j = 0, n = haystack.length(), m = needle.length();
while (i < n) {
if (haystack.charAt(i) == needle.charAt(j)) {
if (j == m - 1) {
System.out.println(i - j);
list.add(i -j);
System.out.println(haystack + " + " + needle + "\n");
j = kmp[j];
}
j++;
i++;
} else {
j = kmp[j];
if (j == -1) {
i++;
j++;
}
}
}
}
}