-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslidingwindowPattern.cpp
More file actions
45 lines (41 loc) · 1.27 KB
/
slidingwindowPattern.cpp
File metadata and controls
45 lines (41 loc) · 1.27 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
#include<bits/stdc++.h>
using namespace std;
//Longest Substring with k distinct characters
int findLength(const string& str, int k) {
int maxLength = 0, winSt = 0;
unordered_map<char, int> freq;
for(int winEnd = 0; winEnd < str.size(); winEnd++){
char rightch = str[winEnd];
freq[rightch]++;
while(freq.size() > k){
char leftch = str[winSt];
freq[leftch]--;
if(freq[leftch] == 0){
freq.erase(leftch);
}
winSt++;
}
maxLength = max(maxLength, winEnd - winSt + 1);
}
return maxLength;
}
//Longest substring with no repeating characters
int norepeat(string str){
int maxLength = 0, st = 0;
unordered_map<char, int> indexMap;
for(int end = 0; end < str.size(); end++){
char rightchar = str[end];
if(indexMap.find(rightchar) != indexMap.end()){
st = max(st, indexMap[rightchar] + 1);
// in the current window, we will not have any 'rightChar' after its
// previous index and if 'windowStart' is already ahead of the last index of 'rightChar',
// we'll keep 'windowStart'
}
indexMap[rightchar] = end;
maxLength = max(maxLength, end - st + 1);
}
return maxLength;
}
int main(){
return 0;
}