easyM Mixed TopicsPattern: String Operations

Substring Replacement Operation Solution

Problem Statement

Given three strings: `source`, `target`, and `replacement`, replace all occurrences of `source` in `target` with `replacement`. If `source` is not found in `target`, return the original `target` string.

Examples

Example 1:
Input:{"source":"abc","target":"abcdabc","replacement":"xyz"}
Output:xyzdxyz
Explanation: Replacing 'abc' in 'abcdabc' with 'xyz' results in 'xyzdxyz'
Example 2:
Input:{"source":"def","target":"abcdefgh","replacement":"ghi"}
Output:abghi
Explanation: Replacing 'def' in 'abcdefgh' with 'ghi' results in 'abghi'

Constraints

  • {"description":"Length of `source`, `target`, and `replacement` must be between 1 and 1000 characters."}
  • {"description":"All input strings consist only of lowercase English letters."}
Time: O(n) Space: O(1)
Analyze constraints and compute optimal solutions step-by-step.

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

No solution code is currently loaded.
Complete this code in the workspace editor.