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:
searchis plain text, not a regular expression:"."matches only a dot.replacementis 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
searchis the empty string, returnstrunchanged. - 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
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 9. ⌘Enter runs, ⌘⇧Enter submits.