Skip to content

Commit 7100e36

Browse files
committed
add: 030
1 parent 8a4378d commit 7100e36

File tree

3 files changed

+166
-9
lines changed

3 files changed

+166
-9
lines changed

README.md

+11-9
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,16 @@
9292

9393
## Hard
9494

95-
| # | Title | Tag |
96-
| :--- | :--------------------------------- | :--------------------------------------- |
97-
| 4 | [Median of Two Sorted Arrays][004] | Array, Binary Search, Divide and Conquer |
98-
| 10 | [Regular Expression Matching][010] | String, Dynamic Programming, Backtracking |
99-
| 23 | [Merge k Sorted Lists][023] | Linked List, Divide and Conquer, Heap |
100-
| 25 | [Reverse Nodes in k-Group][025] | Linked List |
101-
| 44 | [Wildcard Matching][044] | String, Dynamic Programming, Backtracking, Greedy |
102-
| 57 | [Insert Interval][057] | Array, Sort |
103-
| 68 | [Text Justification][068] | String |
95+
| # | Title | Tag |
96+
| :--- | :--------------------------------------- | :--------------------------------------- |
97+
| 4 | [Median of Two Sorted Arrays][004] | Array, Binary Search, Divide and Conquer |
98+
| 10 | [Regular Expression Matching][010] | String, Dynamic Programming, Backtracking |
99+
| 23 | [Merge k Sorted Lists][023] | Linked List, Divide and Conquer, Heap |
100+
| 25 | [Reverse Nodes in k-Group][025] | Linked List |
101+
| 30 | [Substring with Concatenation of All Words][030] | Hash Table, Two Pointers, String |
102+
| 44 | [Wildcard Matching][044] | String, Dynamic Programming, Backtracking, Greedy |
103+
| 57 | [Insert Interval][057] | Array, Sort |
104+
| 68 | [Text Justification][068] | String |
104105

105106

106107

@@ -169,6 +170,7 @@
169170
[010]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/010/README.md
170171
[023]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/023/README.md
171172
[025]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/025/README.md
173+
[030]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/030/README.md
172174
[044]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/044/README.md
173175
[057]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/057/README.md
174176
[068]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/068/README.md

note/030/README.md

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# [Substring with Concatenation of All Words][title]
2+
3+
## Description
4+
5+
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.
6+
7+
8+
For example, given:
9+
10+
**s**: `"barfoothefoobarman"`
11+
12+
**words**: `["foo", "bar"]`
13+
14+
You should return the indices: `[0,9]`.
15+
16+
(order does not matter).
17+
18+
**Tags:** Hash Table, Two Pointers, String
19+
20+
21+
## 思路
22+
23+
题意是
24+
25+
```java
26+
27+
```
28+
29+
30+
## 结语
31+
32+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-java-leetcode][ajl]
33+
34+
35+
36+
[title]: https://leetcode.com/problems/substring-with-concatenation-of-all-words
37+
[ajl]: https://github.com/Blankj/awesome-java-leetcode
+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package com.blankj.hard._030;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.HashMap;
6+
import java.util.HashSet;
7+
import java.util.List;
8+
import java.util.Map;
9+
import java.util.Set;
10+
11+
/**
12+
* <pre>
13+
* author: Blankj
14+
* blog : http://blankj.com
15+
* time : 2018/02/01
16+
* desc :
17+
* </pre>
18+
*/
19+
public class Solution {
20+
public List<Integer> findSubstring(String s, String[] words) {
21+
int wordsSize = words.length, wordLen = words[0].length(), end = s.length() - wordsSize * wordLen;
22+
if (end < 0) return Collections.emptyList();
23+
Map<String, Integer> countMap = new HashMap<>();
24+
for (String word : words) {
25+
countMap.put(word, countMap.getOrDefault(word, 0) + 1);
26+
}
27+
List<Integer> res = new ArrayList<>();
28+
Set<Integer> ignores = new HashSet<>();
29+
for (int i = 0; i <= end; ++i) {
30+
if (ignores.contains(i)) continue;
31+
Map<String, Integer> findMap = new HashMap<>();
32+
int st = i, count = 0;
33+
List<Integer> ignore = new ArrayList<>();
34+
for (int j = 0; ; ++j) {
35+
int cur = i + j * wordLen;
36+
if (cur + wordLen > s.length()) break;
37+
String word = s.substring(cur, cur + wordLen);
38+
if (countMap.containsKey(word)) {
39+
findMap.put(word, findMap.getOrDefault(word, 0) + 1);
40+
++count;
41+
while (findMap.get(word) > countMap.get(word)) {
42+
ignore.add(st);
43+
String tmp = s.substring(st, st += wordLen);
44+
findMap.put(tmp, findMap.get(tmp) - 1);
45+
--count;
46+
}
47+
if (count == wordsSize) {
48+
ignore.add(st);
49+
res.add(st);
50+
String tmp = s.substring(st, st += wordLen);
51+
findMap.put(tmp, findMap.get(tmp) - 1);
52+
--count;
53+
}
54+
} else {
55+
for (int k = i; k <= cur; k += wordLen) {
56+
ignore.add(k);
57+
}
58+
break;
59+
}
60+
}
61+
ignores.addAll(ignore);
62+
}
63+
return res;
64+
}
65+
66+
// public List<Integer> findSubstring(String S, String[] L) {
67+
// List<Integer> res = new LinkedList<>();
68+
// int N = S.length();
69+
// int M = L.length; // *** length
70+
// int wl = L[0].length();
71+
// Map<String, Integer> map = new HashMap<>(), curMap = new HashMap<>();
72+
// for (String s : L) {
73+
// if (map.containsKey(s)) map.put(s, map.get(s) + 1);
74+
// else map.put(s, 1);
75+
// }
76+
// String str = null, tmp = null;
77+
// for (int i = 0; i < wl; i++) {
78+
// int count = 0; // remark: reset count
79+
// int start = i;
80+
// for (int r = i; r + wl <= N; r += wl) {
81+
// str = S.substring(r, r + wl);
82+
// if (map.containsKey(str)) {
83+
// if (curMap.containsKey(str)) curMap.put(str, curMap.get(str) + 1);
84+
// else curMap.put(str, 1);
85+
//
86+
// if (curMap.get(str) <= map.get(str)) count++;
87+
// if (count == M) {
88+
// res.add(start);
89+
// tmp = S.substring(start, start + wl);
90+
// curMap.put(tmp, curMap.get(tmp) - 1);
91+
// start += wl;
92+
// count--;
93+
// }
94+
// while (curMap.get(str) > map.get(str)) {
95+
// tmp = S.substring(start, start + wl);
96+
// curMap.put(tmp, curMap.get(tmp) - 1);
97+
// start += wl;
98+
// if (curMap.get(tmp) < map.get(tmp)) count--;
99+
//
100+
// }
101+
// } else {
102+
// curMap.clear();
103+
// count = 0;
104+
// start = r + wl;
105+
// }
106+
// }
107+
// curMap.clear();
108+
// }
109+
// return res;
110+
// }
111+
112+
public static void main(String[] args) {
113+
Solution solution = new Solution();
114+
System.out.println(solution.findSubstring("wordgoodgoodgoodbestword", new String[]{"word", "good", "best", "good"}));
115+
System.out.println(solution.findSubstring("barfoothefoobarman", new String[]{"foo", "bar"}));
116+
System.out.println(solution.findSubstring("barfoofoobarthefoobarman", new String[]{"bar", "foo", "the"}));
117+
}
118+
}

0 commit comments

Comments
 (0)