-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathString Window.cpp
More file actions
49 lines (46 loc) · 1.25 KB
/
Copy pathString Window.cpp
File metadata and controls
49 lines (46 loc) · 1.25 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
#include "iostream"
#include "string"
#include "climits"
using namespace std;
string findSubstring(string str, string pat) {
int len1 = str.length();
int len2 = pat.length();
int hash_str[256] = {0};
int hash_pat[256] = {0};
for(int i=0;i<len2;i++) {
hash_pat[pat[i]]++;
}
int start = 0, start_index = -1, min_len = INT_MAX, count = 0;
for(int i=0;i<len1;i++) {
hash_str[str[i]]++;
if(hash_pat[str[i]]!=0 and hash_str[str[i]]<=hash_pat[str[i]]){
count++;
}
if(count == len2) {
while(hash_str[str[start]]>hash_pat[str[start]] or hash_pat[str[start]]==0) {
if(hash_str[str[start]]>hash_pat[str[start]]) {
hash_str[str[start]]--;
}
start++;
}
int len_window = i-start+1;
if(len_window<min_len) {
min_len=len_window;
start_index = start;
}
}
}
if(start_index==-1) {
return "No String";
} else {
return str.substr(start_index, min_len);
}
}
int main(int argc, char* argv[])
{
string str, pat;
getline(cin, str);
getline(cin, pat);
cout<<findSubstring(str,pat);
return 0;
}