-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.cpp
More file actions
26 lines (21 loc) · 768 Bytes
/
Copy pathsolution.cpp
File metadata and controls
26 lines (21 loc) · 768 Bytes
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
class Solution {
public:
int longestSubstr(string& s, int k) {
vector<int> freq(26, 0); // frequency of characters
int left = 0, maxFreq = 0, maxLen = 0;
for (int right = 0; right < s.size(); right++) {
// Increase frequency of current character
freq[s[right] - 'A']++;
// Update max frequency in window
maxFreq = max(maxFreq, freq[s[right] - 'A']);
// If changes needed > k, shrink window
while ((right - left + 1) - maxFreq > k) {
freq[s[left] - 'A']--;
left++;
}
// Update max length
maxLen = max(maxLen, right - left + 1);
}
return maxLen;
}
};