codekofi
← All problems

Problem 71

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

1int solution(const string& a, const string& b) {
2
3 int m = a.size();
4 int n = b.size();
5
6 vector<int> prev(n + 1, 0);
7 vector<int> curr(n + 1, 0);
8
9 for (int i = 1; i <= m; i++) {
10
11 for (int j = 1; j <= n; j++) {
12
13 if (a[i - 1] == b[j - 1]) {
14 curr[j] = prev[j - 1] + 1;
15 } else {
16 curr[j] = max(prev[j], curr[j - 1]);
17 }
18 }
19
20 swap(prev, curr);
21 }
22
23 return prev[n];
24}

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.