Skip to content
Onsite

Replace All Occurrences in a String

EasyP3Coding

Implement replaceAllOccurrences(str, search, replacement), which returns a copy of str in which every occurrence of the literal text search is replaced by replacement.

Rules:

  • search is plain text, not a regular expression: "." matches only a dot.
  • replacement is inserted literally; sequences such as "$&" have no special meaning.
  • Occurrences are found left to right and do not overlap: replacing "aa" in "aaa" replaces the first two characters only.
  • If search is the empty string, return str unchanged.
  • Do not call String.prototype.replaceAll.

Example 1

Input
replaceAllOccurrences('a-b-c', '-', '+')
Output
'a+b+c'

Constraints

  • 0 <= str.length <= 10^5
  • 0 <= search.length, replacement.length <= 100

Hints

Run checks the 3 visible tests, Submit all 9.