Learn
Exam

Modify String

Difficulty
STARTER

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 word is between 1 and 100 inclusive.
  • The length of oldString and newString is between 1 and the length of word inclusive.
  • The length of oldString and newString may be different.
  • word, oldString, and newString consist 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", and newString = "python", replacing "world" with "python" results in "hello python".

Explanation for Test Case 2

  • When word = "banana split", oldString = "banana", and newString = "apple", replacing "banana" with "apple" results in "apple split".

Explanation for Test Case 3

  • When word = "abcdef", oldString = "cde", and newString = "xyz", replacing "cde" with "xyz" results in "abxyzf".