codekofi
← All problems

Problem 118

Hard

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& s, const string& p) {
2
3 int m = s.size();
4 int n = p.size();
5
6 vector<vector<bool>> ok(m + 1, vector<bool>(n + 1, false));
7 ok[0][0] = true;
8
9 for (int j = 2; j <= n; j++) {
10
11 if (p[j - 1] == '*') {
12 ok[0][j] = ok[0][j - 2];
13 }
14 }
15
16 for (int i = 1; i <= m; i++) {
17 for (int j = 1; j <= n; j++) {
18
19 if (p[j - 1] == '*') {
20
21 if (j < 2) {
22 continue;
23 }
24
25 bool skip = ok[i][j - 2];
26 bool same = p[j - 2] == '.' || p[j - 2] == s[i - 1];
27
28 ok[i][j] = skip || (same && ok[i - 1][j]);
29
30 } else if (p[j - 1] == '.' || p[j - 1] == s[i - 1]) {
31 ok[i][j] = ok[i - 1][j - 1];
32 }
33 }
34 }
35
36 return ok[m][n];
37}

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.