-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindSubstring.cpp
More file actions
executable file
·59 lines (51 loc) · 1.48 KB
/
findSubstring.cpp
File metadata and controls
executable file
·59 lines (51 loc) · 1.48 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
/*
You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening characters.
For example, given:
S: "barfoothefoobarman"
L: ["foo", "bar"]
You should return the indices: [0,9].
(order does not matter).
*/
#include <vector>
#include <string>
#include <algorithm>
#include <map>
#include <iostream>
#include <unordered_map>
using namespace std;
vector<int> findSubstring(string s, vector<string> &L){
vector<int> res;
int n = L.size();
if (n <= 0 || s.size() < n*L[0].size())
return res;
int m = L[0].size();
unordered_map<string,int> Lwords;
unordered_map<string, int> Cwords;
for(int i = 0; i < n; ++i)
++Lwords[L[i]];
for(int i = 0; i < s.size() - m*n+1; ++i){
if(!Cwords.empty())
Cwords.clear();
int j = 0;
for(j = 0; j < n; ++j){
string tp = s.substr(i+j*m, m);
if(Lwords.find(tp) == Lwords.end())
break;
++Cwords[tp];
if(Cwords[tp] > Lwords[tp])
break;
}
if (j == n) res.push_back(i);
}
return res;
}
int main(int argc, char const *argv[])
{
vector<string> L = {"foo", "bar"};
string s = "barfoothefoobarmanbarfoobarfoo";
vector<int> bp = findSubstring(s,L);
for(auto &x : bp)
cout << x <<" ";
cout << endl;
return 0;
}