BackeasyM Mixed Topics Cognizant

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.

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."}
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free