2018-09-30 22:01:58 +00:00
|
|
|
---
|
|
|
|
id: 596fda99c69f779975a1b67d
|
2020-11-27 18:02:05 +00:00
|
|
|
title: Count occurrences of a substring
|
2018-09-30 22:01:58 +00:00
|
|
|
challengeType: 5
|
2019-08-05 16:17:33 +00:00
|
|
|
forumTopicId: 302237
|
2021-01-13 02:31:00 +00:00
|
|
|
dashedName: count-occurrences-of-a-substring
|
2018-09-30 22:01:58 +00:00
|
|
|
---
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
# --description--
|
|
|
|
|
2019-02-25 04:36:09 +00:00
|
|
|
Create a function, or show a built-in function, to count the number of non-overlapping occurrences of a substring inside a string.
|
2020-11-27 18:02:05 +00:00
|
|
|
|
2019-02-25 04:36:09 +00:00
|
|
|
The function should take two arguments:
|
2020-11-27 18:02:05 +00:00
|
|
|
|
2019-02-25 04:36:09 +00:00
|
|
|
<ul>
|
|
|
|
<li>the first argument being the string to search, and</li>
|
|
|
|
<li>the second a substring to be searched for.</li>
|
|
|
|
</ul>
|
2020-11-27 18:02:05 +00:00
|
|
|
|
2019-02-25 04:36:09 +00:00
|
|
|
It should return an integer count.
|
2020-11-27 18:02:05 +00:00
|
|
|
|
2019-02-25 04:36:09 +00:00
|
|
|
The matching should yield the highest number of non-overlapping matches.
|
2020-11-27 18:02:05 +00:00
|
|
|
|
2019-02-25 04:36:09 +00:00
|
|
|
In general, this essentially means matching from left-to-right or right-to-left.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
# --hints--
|
2018-09-30 22:01:58 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
`countSubstring` should be a function.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
```js
|
|
|
|
assert(typeof countSubstring === 'function');
|
|
|
|
```
|
2018-09-30 22:01:58 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
`countSubstring("the three truths", "th")` should return `3`.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
```js
|
|
|
|
assert.equal(countSubstring(testCases[0], searchString[0]), results[0]);
|
2018-09-30 22:01:58 +00:00
|
|
|
```
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
`countSubstring("ababababab", "abab")` should return `2`.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
```js
|
|
|
|
assert.equal(countSubstring(testCases[1], searchString[1]), results[1]);
|
|
|
|
```
|
2018-09-30 22:01:58 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
`countSubstring("abaabba*bbaba*bbab", "a*b")` should return `2`.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
|
|
```js
|
2020-11-27 18:02:05 +00:00
|
|
|
assert.equal(countSubstring(testCases[2], searchString[2]), results[2]);
|
2018-09-30 22:01:58 +00:00
|
|
|
```
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
# --seed--
|
2018-09-30 22:01:58 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
## --after-user-code--
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
|
|
```js
|
2018-10-20 18:02:47 +00:00
|
|
|
const testCases = ['the three truths', 'ababababab', 'abaabba*bbaba*bbab'];
|
|
|
|
const searchString = ['th', 'abab', 'a*b'];
|
|
|
|
const results = [3, 2, 2];
|
2018-09-30 22:01:58 +00:00
|
|
|
```
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
## --seed-contents--
|
2018-09-30 22:01:58 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
```js
|
|
|
|
function countSubstring(str, subStr) {
|
2018-09-30 22:01:58 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
```
|
2018-09-30 22:01:58 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
# --solutions--
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
function countSubstring(str, subStr) {
|
|
|
|
const escapedSubStr = subStr.replace(/[.+*?^$[\]{}()|/]/g, '\\$&');
|
|
|
|
const matches = str.match(new RegExp(escapedSubStr, 'g'));
|
|
|
|
return matches ? matches.length : 0;
|
|
|
|
}
|
|
|
|
```
|