codekofi
← All problems

Problem 105

Medium

1 · Worked examples

0 / 3

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 3 outputs correctly.

The accepted solution

1vector<int> solution(const string& s) {
2
3 int n = s.size();
4 vector<int> last(26, -1);
5
6 for (int i = 0; i < n; i++) {
7 last[s[i] - 'a'] = i;
8 }
9
10 vector<int> ans;
11
12 int start = 0;
13 int edge = 0;
14
15 for (int i = 0; i < n; i++) {
16
17 edge = max(edge, last[s[i] - 'a']);
18
19 if (i == edge) {
20 ans.push_back(i - start + 1);
21 start = i + 1;
22 }
23 }
24
25 return ans;
26}

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.