codekofi
← All problems

Problem 90

Medium

1 · Worked examples

0 / 4

Type an input and write what you think is the output . Each problem is converted to Web Assembly, so any possible input will show the corresponding output. Only correct predictions count.

solution()
returns

2 · Which problem is it?

Locked until you have predicted 4 outputs correctly.

The accepted solution

1int solution(const string& s, int k) {
2
3 int n = s.size();
4 vector<int> count(26, 0);
5
6 int left = 0;
7 int most = 0;
8 int ans = 0;
9
10 for (int right = 0; right < n; right++) {
11
12 count[s[right] - 'A']++;
13 most = max(most, count[s[right] - 'A']);
14
15 while (right - left + 1 - most > k) {
16 count[s[left] - 'A']--;
17 left++;
18 }
19
20 int width = right - left + 1;
21 ans = max(ans, width);
22 }
23
24 return ans;
25}

Names have been stripped. The signature is the only clue you get for free. Compiled as C++20 with the standard headers and using namespace std; already in scope.