codekofi
← All problems

Problem 93

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& s) {
2
3 int n = s.size();
4 int ans = 0;
5
6 for (int mid = 0; mid < n; mid++) {
7 for (int shift = 0; shift < 2; shift++) {
8
9 int lo = mid;
10 int hi = mid + shift;
11
12 while (lo >= 0 && hi < n && s[lo] == s[hi]) {
13 ans++;
14 lo--;
15 hi++;
16 }
17 }
18 }
19
20 return ans;
21}

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.