Problem
Given a string word, a substring oldString, and a replacement string newString, write a function solution that returns the result of replacing oldString in word with newString. Note that oldString appears only once in word.
Constraints
- The length of
wordis between 1 and 100 inclusive. - The length of
oldStringandnewStringis between 1 and the length ofwordinclusive. - The length of
oldStringandnewStringmay be different. word,oldString, andnewStringconsist only of lowercase English letters.
Test Case
| Test Case Number | word | oldString | newString | return |
|---|---|---|---|---|
| 1 | "hello world" | "world" | "python" | "hello python" |
| 2 | "banana split" | "banana" | "apple" | "apple split" |
| 3 | "abcdef" | "cde" | "xyz" | "abxyzf" |
Explanation for Test Case 1
- When
word = "hello world",oldString = "world", andnewString = "python", replacing"world"with"python"results in"hello python".
Explanation for Test Case 2
- When
word = "banana split",oldString = "banana", andnewString = "apple", replacing"banana"with"apple"results in"apple split".
Explanation for Test Case 3
- When
word = "abcdef",oldString = "cde", andnewString = "xyz", replacing"cde"with"xyz"results in"abxyzf".