codekofi
← All problems

Problem 70

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

1bool solution(const string& a, const string& b,
2 const string& c) {
3
4 int m = a.size();
5 int n = b.size();
6
7 if ((int)c.size() != m + n) {
8 return false;
9 }
10
11 vector<bool> row(n + 1, false);
12 row[0] = true;
13
14 for (int j = 1; j <= n; j++) {
15 row[j] = row[j - 1] && b[j - 1] == c[j - 1];
16 }
17
18 for (int i = 1; i <= m; i++) {
19
20 row[0] = row[0] && a[i - 1] == c[i - 1];
21
22 for (int j = 1; j <= n; j++) {
23
24 bool fromA = row[j] && a[i - 1] == c[i + j - 1];
25 bool fromB = row[j - 1] && b[j - 1] == c[i + j - 1];
26
27 row[j] = fromA || fromB;
28 }
29 }
30
31 return row[n];
32}

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.