-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution1.cpp
33 lines (33 loc) · 931 Bytes
/
Solution1.cpp
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
// Problem: https://leetcode.com/problems/substring-with-concatenation-of-all-words/
// Author: github.com/ankuralld5999
class Solution {
private:
int len, n;
string s;
bool rec(int i, unordered_map<string, int> &m, int cnt) {
if (cnt == n) return true;
int &v = m[s.substr(i, len)];
if (v) {
v--;
bool ret = rec(i + len, m, cnt + 1);
v++;
return ret;
}
return false;
}
public:
vector<int> findSubstring(string s, vector<string>& words) {
if (words.empty()) return {};
this->s = s;
len = words[0].size();
n = words.size();
unordered_map<string, int> m;
for (string word : words) ++m[word];
int end = s.size() - n * len;
vector<int> v;
for (int i = 0; i <= end; ++i) {
if (rec(i, m, 0)) v.push_back(i);
}
return v;
}
};