codekofi
← All problems

Problem 112

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

1string solution(const string& s) {
2
3 if (s.empty()) {
4 return "";
5 }
6
7 int n = s.size();
8 int start = 0;
9 int len = 1;
10
11 for (int mid = 0; mid < n; mid++) {
12 for (int shift = 0; shift < 2; shift++) {
13
14 int lo = mid;
15 int hi = mid + shift;
16
17 while (lo >= 0 && hi < n && s[lo] == s[hi]) {
18
19 int wide = hi - lo + 1;
20
21 if (wide > len) {
22 len = wide;
23 start = lo;
24 }
25
26 lo--;
27 hi++;
28 }
29 }
30 }
31
32 return s.substr(start, len);
33}

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.