2021-06-15 00:49:18 -07:00
|
|
|
---
|
|
|
|
id: 596fda99c69f779975a1b67d
|
2021-08-05 23:31:15 +09:00
|
|
|
title: Contar as ocorrências de uma substring
|
2021-06-15 00:49:18 -07:00
|
|
|
challengeType: 5
|
|
|
|
forumTopicId: 302237
|
|
|
|
dashedName: count-occurrences-of-a-substring
|
|
|
|
---
|
|
|
|
|
|
|
|
# --description--
|
|
|
|
|
2021-08-05 23:31:15 +09:00
|
|
|
Crie uma função, ou mostre uma função integrada para contar o número de ocorrências não sobrepostas de uma substring dentro de uma string.
|
2021-06-15 00:49:18 -07:00
|
|
|
|
2021-08-05 23:31:15 +09:00
|
|
|
A função deve receber dois argumentos:
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
<ul>
|
2021-08-05 23:31:15 +09:00
|
|
|
<li>o primeiro argumento deve ser a string onde ocorrerá a busca, e</li>
|
|
|
|
<li>o segundo deve ser uma substring a ser buscada.</li>
|
2021-06-15 00:49:18 -07:00
|
|
|
</ul>
|
|
|
|
|
2021-08-05 23:31:15 +09:00
|
|
|
A função deve retornar uma contagem inteira.
|
2021-06-15 00:49:18 -07:00
|
|
|
|
2021-08-05 23:31:15 +09:00
|
|
|
A pesquisa deve produzir o maior número de correspondências não sobrepostas.
|
2021-06-15 00:49:18 -07:00
|
|
|
|
2021-08-05 23:31:15 +09:00
|
|
|
Em geral, isto significa, essencialmente, uma correspondência da esquerda para a direita e da direita para a esquerda.
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
# --hints--
|
|
|
|
|
2021-08-05 23:31:15 +09:00
|
|
|
`countSubstring` deve ser uma função.
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(typeof countSubstring === 'function');
|
|
|
|
```
|
|
|
|
|
2021-08-05 23:31:15 +09:00
|
|
|
`countSubstring("the three truths", "th")` deve retornar `3`.
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert.equal(countSubstring(testCases[0], searchString[0]), results[0]);
|
|
|
|
```
|
|
|
|
|
2021-08-05 23:31:15 +09:00
|
|
|
`countSubstring("ababababab", "abab")` deve retornar `2`.
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert.equal(countSubstring(testCases[1], searchString[1]), results[1]);
|
|
|
|
```
|
|
|
|
|
2021-08-05 23:31:15 +09:00
|
|
|
`countSubstring("abaabba*bbaba*bbab", "a*b")` deve retornar `2`.
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert.equal(countSubstring(testCases[2], searchString[2]), results[2]);
|
|
|
|
```
|
|
|
|
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --after-user-code--
|
|
|
|
|
|
|
|
```js
|
|
|
|
const testCases = ['the three truths', 'ababababab', 'abaabba*bbaba*bbab'];
|
|
|
|
const searchString = ['th', 'abab', 'a*b'];
|
|
|
|
const results = [3, 2, 2];
|
|
|
|
```
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```js
|
|
|
|
function countSubstring(str, subStr) {
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
# --solutions--
|
|
|
|
|
|
|
|
```js
|
|
|
|
function countSubstring(str, subStr) {
|
|
|
|
const escapedSubStr = subStr.replace(/[.+*?^$[\]{}()|/]/g, '\\$&');
|
|
|
|
const matches = str.match(new RegExp(escapedSubStr, 'g'));
|
|
|
|
return matches ? matches.length : 0;
|
|
|
|
}
|
|
|
|
```
|