-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathminSubString.cpp
77 lines (63 loc) · 1.67 KB
/
minSubString.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
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
/*
* @author : imkaka
* @date : 2/1/2019
*/
#include<bits/stdc++.h>
using namespace std;
string minSubString(string str, vector<char> arr){
// Corner case
int len = str.size();
if(arr.size() > len)
{
// cout << "No Answer" << endl;
return "No Answer";
}
set<char> s;
for(char c : arr){
s.insert(c);
}
int lenMin = len+1;
string result = "";
for(int i = 0; i < len; ++i){
set<char>tempS = s;
int temp = 0;
string tempStr = "";
for(int j = i; j < len; ++j){
if(tempS.find(str[j]) != tempS.end()){
tempS.erase(str[j]);
temp++;
tempStr += str[j];
}
else{
temp++;
tempStr += str[j];
}
if(tempS.empty()){
if(lenMin > temp){
lenMin = temp;
result = tempStr;
}
break;
}
}
}
return result;
}
int main(){
string str = "abyuxabyteqaebczt";
vector<char> arr = {'a', 'b', 'c'};
if(minSubString(str, arr) != "")
cout << "Substring: " << minSubString(str, arr) << endl;
else cout << "No Sollution" << endl;
string str2 = "abx";
vector<char> arr2 = {'a', 'b', 'c'};
if(minSubString(str2, arr2) != "")
cout << "Substring: " << minSubString(str2, arr2) << endl;
else cout << "No Sollution" << endl;
string str3 = "x";
vector<char> arr3 = {'x'};
if(minSubString(str3, arr3) != "")
cout << "Substring: " << minSubString(str3, arr3) << endl;
else cout << "No Sollution" << endl;
return 0;
}