codekofi
← All problems

Problem 11

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) {
2
3 int n = s.size();
4 unordered_map<char, int> lastSeen;
5
6 int start = 0;
7 int ans = 0;
8
9 for (int i = 0; i < n; i++) {
10
11 char c = s[i];
12
13 if (lastSeen.count(c) && lastSeen[c] >= start) {
14 start = lastSeen[c] + 1;
15 }
16
17 lastSeen[c] = i;
18
19 int width = i - start + 1;
20
21 if (width > ans) {
22 ans = width;
23 }
24 }
25
26 return ans;
27}

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.