-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path#103.cpp
80 lines (65 loc) · 2.03 KB
/
#103.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
78
79
80
#include<iostream>
#include<unordered_set>
#include<unordered_map>
#include<string>
#include<limits.h>
#include<stdlib.h>
using namespace std;
bool is_in_set(unordered_set<char>& S, char c) {
return S.find(c) != S.end();
}
bool is_in_map(unordered_map<char, int>& M, char c) {
return M.find(c) != M.end();
}
void inc_map(unordered_map<char, int>& M, char c) {
if (is_in_map(M, c)) M[c] += 1;
else M[c] = 1;
}
int next_index_with_chars(unordered_set<char>& chars, string s, int start) {
for (int i = start; i < s.length(); i++) {
if (is_in_set(chars, s[i])) return i;
}
return -1;
}
string shortest_substr_with_chars(unordered_set<char>& chars, string s) {
int left = next_index_with_chars(chars, s, 0);
int right = left;
unordered_map<char, int> curr_state = {{s[left], 1}};
string shortest = s;
while(right < s.length() - 1) {
// substring found with all chars present
// update shortest if new substring is shorter
if (curr_state.size() == chars.size() && (right-left+1) < shortest.length())
shortest = s.substr(left, right-left+1);
// increment window
right++;
// new char added is in set
if (is_in_set(chars, s[right])) {
inc_map(curr_state, s[right]); // add the new char to map
// New char is same as the leftmost char in the window.
// We can shrink the window from the left since we only
// need 1 of each char in the substring
if (s[left] == s[right]) {
// keep shrinking until found char with 1 occurence
while (curr_state[s[left]] != 1) {
curr_state[s[left]] -= 1;
left = next_index_with_chars(chars, s, left+1);
}
}
}
}
// need to check for last window
if (curr_state.size() == chars.size() && (s.length()-left) < shortest.length())
shortest = s.substr(left, s.length()-left);
return ((shortest.length() == 0) ? "" : shortest);
}
int main() {
unordered_set<char> chars({'a','e','c'});
string s = "bdefacee";
string ret = shortest_substr_with_chars(chars, s);
if (ret.length() == 0) {
cout << "No possible substring." << endl;
} else {
cout << ret << endl;
}
}