-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindSubstring.cpp
49 lines (45 loc) · 1.1 KB
/
findSubstring.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "findSubstring.h"
#include <cstring>
#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
vector<int> Solution::findSubstring(string S, vector<string> &L) {
vector<int> res;
map<string, int> expected_count;
map<string, int> current_count;
for (int i = 0; i < L.size(); ++i)
{
expected_count[L[i]]++;
}
if(L.size()==0)
return res;
int n = L.size()*L[0].length(), m = L.size(), l = L[0].length(), slen = S.length();
cout<<n<<' '<<m<<' '<<l<<' '<<S.length()<<endl;
int start = 0;
while(start<=(slen - n)) {
cout<<"In the Loop"<<endl;
cout<<"Now start and S.length()-n :"<<start<<' '<<slen - n<<endl;
string sub = S.substr(start,n);
bool isSubstring = true;
current_count.clear();
for (int i = 0; i < m; ++i)
{
string subsub = sub.substr(i*l, l);
if(expected_count.find(subsub) != expected_count.end()){
current_count[subsub]++;
}else {
isSubstring = false;
break;
}
if(current_count[subsub]>expected_count[subsub]){
isSubstring = false;
break;
}
}
if(isSubstring)
res.push_back(start);
start++;
}
return res;
}