-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
200 lines (180 loc) · 5.66 KB
/
main.cpp
File metadata and controls
200 lines (180 loc) · 5.66 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Source: https://leetcode.com/problems/substring-with-concatenation-of-all-words
// Title: Substring with Concatenation of All Words
// Difficulty: Hard
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// You are given a string `s` and an array of strings `words`. All the strings of `words` are of **the same length**.
//
// A **concatenated string** is a string that exactly contains all the strings of any permutation of `words` concatenated.
//
// - For example, if `words = ["ab","cd","ef"]`, then `"abcdef"`, `"abefcd"`, `"cdabef"`, `"cdefab"`, `"efabcd"`, and `"efcdab"` are all concatenated strings. `"acdbef"` is not a concatenated string because it is not the concatenation of any permutation of `words`.
//
// Return an array of the starting indices of all the concatenated substrings in `s`. You can return the answer in **any order**.
//
// **Example 1:**
//
// ```
// Input: s = "barfoothefoobarman", words = ["foo","bar"]
//
// Output: [0,9]
//
// Explanation:
//
// The substring starting at 0 is `"barfoo"`. It is the concatenation of `["bar","foo"]` which is a permutation of `words`.<br>
// The substring starting at 9 is `"foobar"`. It is the concatenation of `["foo","bar"]` which is a permutation of `words`.
//
// **Example 2:**
//
// ```
// Input: s = "wordgoodgoodgoodbestword", words = ["word","good","best","word"]
// Output: []
// Explanation:
// There is no concatenated substring.
// ```
//
// **Example 3:**
//
// ```
// Input: s = "barfoofoobarthefoobarman", words = ["bar","foo","the"]
// Output: [6,9,12]
// Explanation:
// The substring starting at 6 is `"foobarthe"`. It is the concatenation of `["foo","bar","the"]`.<br>
// The substring starting at 9 is `"barthefoo"`. It is the concatenation of `["bar","the","foo"]`.<br>
// The substring starting at 12 is `"thefoobar"`. It is the concatenation of `["the","foo","bar"]`.
// ```
//
// **Constraints:**
//
// - `1 <= s.length <= 10^4`
// - `1 <= words.length <= 5000`
// - `1 <= words[i].length <= 30`
// - `s` and `words[i]` consist of lowercase English letters.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <algorithm>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
// Use Hash Map
class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
int n = words.size();
int l = words[0].size();
int m = s.size();
if (m - n * l < 0) {
return {};
}
// Count words
unordered_map<string, int> wordMap;
vector<int> wordCounter(n + 1);
auto wordCount = 1; // reserve 0 for invalid word
for (auto i = 0; i < n; i++) {
auto& word = words[i];
if (!wordMap.count(word)) {
wordMap[word] = wordCount;
wordCount++;
}
wordCounter[wordMap[word]]++;
}
// Check word in string
vector<int> sIdx(m - l + 1);
for (auto i = 0; i <= m - l; i++) {
auto word = s.substr(i, l);
if (wordMap.count(word)) {
sIdx[i] = wordMap[word];
}
}
// Find answer
vector<int> ans;
for (auto i = 0; i <= m - n * l; i++) {
if (sIdx[i] == 0) {
continue;
}
vector<int> counter(wordCounter); // copy counter
auto valid = true;
for (auto j = i; j < i + n * l; j += l) {
auto idx = sIdx[j];
if (counter[idx] == 0) {
valid = false;
break;
}
counter[idx]--;
}
if (valid) {
ans.push_back(i);
}
}
return ans;
}
};
// Use Hash Map + Sliding Window
//
// Let l be the length of the word.
// Use sliding window on each starting in [0, l).
class Solution2 {
public:
vector<int> findSubstring(string s, vector<string>& words) {
int n = words.size();
int l = words[0].size();
int m = s.size();
if (m - n * l < 0) {
return {};
}
// Count words
unordered_map<string, int> wordMap;
vector<int> wordCounter(n + 1);
auto wordCount = 1; // reserve 0 for invalid word
for (auto i = 0; i < n; i++) {
auto& word = words[i];
if (!wordMap.count(word)) {
wordMap[word] = wordCount;
wordCount++;
}
wordCounter[wordMap[word]]++;
}
// Check word in string
vector<int> sIdx(m + 1);
for (auto i = 0; i + l <= m; i++) {
auto word = s.substr(i, l);
if (wordMap.count(word)) {
sIdx[i] = wordMap[word];
}
}
// Sliding Window
vector<int> ans;
vector<int> ansCounter(wordCount);
for (auto start = 0; start < l; start++) {
fill(ansCounter.begin(), ansCounter.end(), 0);
auto left = start;
auto right = start + n * l;
int valids = 1; // invalid count is zero
for (auto j = left; j < right; j += l) {
auto idx = sIdx[j];
if (ansCounter[idx] == wordCounter[idx]) valids--;
ansCounter[idx]++;
if (ansCounter[idx] == wordCounter[idx]) valids++;
}
while (right <= m) {
if (valids == wordCount) {
ans.push_back(left);
}
// remove left
auto leftIdx = sIdx[left];
if (ansCounter[leftIdx] == wordCounter[leftIdx]) valids--;
ansCounter[leftIdx]--;
if (ansCounter[leftIdx] == wordCounter[leftIdx]) valids++;
// add right
auto rightIdx = sIdx[right];
if (ansCounter[rightIdx] == wordCounter[rightIdx]) valids--;
ansCounter[rightIdx]++;
if (ansCounter[rightIdx] == wordCounter[rightIdx]) valids++;
// shift
left += l;
right += l;
}
}
return ans;
}
};