codekofi
← All problems

Problem 34

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 mirrored(const string& s, int lo, int hi) {
2
3 while (lo < hi) {
4
5 if (s[lo] != s[hi]) {
6 return false;
7 }
8
9 lo++;
10 hi--;
11 }
12
13 return true;
14}
15
16void cut(const string& s, int i, vector<string>& current,
17 vector<vector<string>>& out) {
18
19 int n = s.size();
20
21 if (i == n) {
22 out.push_back(current);
23 return;
24 }
25
26 for (int j = i; j < n; j++) {
27
28 if (!mirrored(s, i, j)) {
29 continue;
30 }
31
32 string piece = s.substr(i, j - i + 1);
33
34 current.push_back(piece);
35 cut(s, j + 1, current, out);
36 current.pop_back();
37 }
38}
39
40vector<vector<string>> solution(const string& s) {
41
42 vector<vector<string>> ans;
43 vector<string> current;
44
45 cut(s, 0, current, ans);
46
47 return ans;
48}

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.