codekofi
← All problems

Problem 11

Medium40 XP

1 · Worked examples

0 / 4

Give it an input and say what you think comes back. It compiles and runs for real, so a wrong guess still shows you the answer — probe as much as you like. 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 int n = s.size();
3 unordered_map<char, int> lastSeen;
4 int start = 0;
5 int best = 0;
6 for (int i = 0; i < n; i++) {
7 char c = s[i];
8 if (lastSeen.count(c) && lastSeen[c] >= start) {
9 start = lastSeen[c] + 1;
10 }
11 lastSeen[c] = i;
12 int width = i - start + 1;
13 if (width > best) best = width;
14 }
15 return best;
16}

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.