-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSliding_Window_Algorithm.cpp
60 lines (42 loc) · 1.33 KB
/
Sliding_Window_Algorithm.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
/*
Find the longest substring of a string containing `k` distinct characters
Given a string and a positive number k, find the longest substring of the string containing k distinct characters. If k is more than the total number of distinct characters in the string, return the whole string.
The problem differs from the problem of finding the longest subsequence with k distinct characters. Unlike subsequences, substrings are required to occupy consecutive positions within the original string.
For example, consider string abcbdbdbbdcdabd.
For k = 2, o/p is ‘bdbdbbd’
For k = 3, o/p is ‘bcbdbdbbdcd’
For k = 5, o/p is ‘abcbdbdbbdcdabd’
*/
#include <bits/stdc++.h>
using namespace std;
int main() {
int k , ans = 0 , ind1 = 0 , ind2 = 0;
string s;
cin >> k >> s ;
map<char , int> mp;
int n = s.size();
int low = 0 , high = 0;
mp[s[0]]++;
while (high <= n && low >= 0) {
if (mp.size() < k) {
high++;
mp[s[high]]++;
}
else if (mp.size() > k) {
mp[s[low]]--;
if (mp[s[low]] == 0) mp.erase(s[low]);
low++;
}
else if (mp.size() == k) {
if (ans < high - low + 1) {
ans = high - low + 1 ;
ind1 = low;
ind2 = high;
}
high++;
mp[s[high]]++;
}
}
cout << ans << " " << s.substr(ind1 , ind2 - ind1 + 1) << endl;
return 0;
}