Oliver Eyton-Williams ee1e8abd87
feat(curriculum): restore seed + solution to Chinese (#40683)
* feat(tools): add seed/solution restore script

* chore(curriculum): remove empty sections' markers

* chore(curriculum): add seed + solution to Chinese

* chore: remove old formatter

* fix: update getChallenges

parse translated challenges separately, without reference to the source

* chore(curriculum): add dashedName to English

* chore(curriculum): add dashedName to Chinese

* refactor: remove unused challenge property 'name'

* fix: relax dashedName requirement

* fix: stray tag

Remove stray `pre` tag from challenge file.

Signed-off-by: nhcarrigan <nhcarrigan@gmail.com>

Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2021-01-12 19:31:00 -07:00

1.7 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
596fda99c69f779975a1b67d Count occurrences of a substring 5 302237 count-occurrences-of-a-substring

--description--

Create a function, or show a built-in function, to count the number of non-overlapping occurrences of a substring inside a string.

The function should take two arguments:

  • the first argument being the string to search, and
  • the second a substring to be searched for.

It should return an integer count.

The matching should yield the highest number of non-overlapping matches.

In general, this essentially means matching from left-to-right or right-to-left.

--hints--

countSubstring should be a function.

assert(typeof countSubstring === 'function');

countSubstring("the three truths", "th") should return 3.

assert.equal(countSubstring(testCases[0], searchString[0]), results[0]);

countSubstring("ababababab", "abab") should return 2.

assert.equal(countSubstring(testCases[1], searchString[1]), results[1]);

countSubstring("abaabba*bbaba*bbab", "a*b") should return 2.

assert.equal(countSubstring(testCases[2], searchString[2]), results[2]);

--seed--

--after-user-code--

const testCases = ['the three truths', 'ababababab', 'abaabba*bbaba*bbab'];
const searchString = ['th', 'abab', 'a*b'];
const results = [3, 2, 2];

--seed-contents--

function countSubstring(str, subStr) {

  return true;
}

--solutions--

function countSubstring(str, subStr) {
  const escapedSubStr = subStr.replace(/[.+*?^$[\]{}()|/]/g, '\\$&');
  const matches = str.match(new RegExp(escapedSubStr, 'g'));
  return matches ? matches.length : 0;
}