Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

72 lines
1.3 KiB
Markdown

---
id: 68cae5b538ff798bbd4da003
title: "Challenge 65: String Count"
challengeType: 28
dashedName: challenge-65
---
# --description--
Given two strings, determine how many times the second string appears in the first.
- The pattern string can overlap in the first string. For example, `"aaa"` contains `"aa"` twice. The first two `a`'s and the second two.
# --hints--
`count('abcdefg', 'def')` should return `1`.
```js
assert.equal(count('abcdefg', 'def'), 1);
```
`count('hello', 'world')` should return `0`.
```js
assert.equal(count('hello', 'world'), 0);
```
`count('mississippi', 'iss')` should return `2`.
```js
assert.equal(count('mississippi', 'iss'), 2);
```
`count('she sells seashells by the seashore', 'sh')` should return `3`.
```js
assert.equal(count('she sells seashells by the seashore', 'sh'), 3);
```
`count('101010101010101010101', '101')` should return `10`.
```js
assert.equal(count('101010101010101010101', '101'), 10);
```
# --seed--
## --seed-contents--
```js
function count(text, pattern) {
return text;
}
```
# --solutions--
```js
function count(text, pattern) {
let occurrences = 0;
for (let i = 0; i <= text.length - pattern.length; i++) {
if (text.slice(i, i + pattern.length) === pattern) {
occurrences++;
}
}
return occurrences;
}
```