codekofi
← All problems

Problem 50

Medium

1 · Worked examples

0 / 4

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 4 outputs correctly.

The accepted solution

1bool solution(const string& pattern, const string& text) {
2
3 int m = pattern.size();
4 int n = text.size();
5
6 if (m > n) {
7 return false;
8 }
9
10 vector<int> need(26, 0);
11 vector<int> have(26, 0);
12
13 for (char c : pattern) {
14 need[c - 'a']++;
15 }
16
17 for (int i = 0; i < m; i++) {
18 have[text[i] - 'a']++;
19 }
20
21 if (need == have) {
22 return true;
23 }
24
25 for (int i = m; i < n; i++) {
26
27 have[text[i] - 'a']++;
28 have[text[i - m] - 'a']--;
29
30 if (need == have) {
31 return true;
32 }
33 }
34
35 return false;
36}

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.