X

DAYS

:

HOUR

:

MINS

:

SEC

Copied to Clipboard
Replace a word
Easy Accuracy: 68.79% Submissions: 1380 Points: 2

Given three strings S, oldW and newW. Find all occurrences of the word oldW in S and replace them with word newW.

Example 1:

Input: 
S = "xxforxx xx for xx"
oldW = "xx"
newW = "Geeks"
Output: 
"geeksforgeeks geeks for geeksExplanation: 
Replacing each "xx" with
"Geeks" in S.

Example 2:

Input: 
S = "india is the xx country"
oldW = "xx"
newW = "best"
Output: 
"india is the best country"
Explanation: 
Replacing each "xx" with
"best" in S.


Your Task:  
You dont need to read input or print anything. Complete the function ReplaceAll() which takes S, oldW and newW as input parameters and returns the string S after replacing all oldW with newW.

Expected Time Complexity: O(n2)
Expected Auxiliary Space: O(1)

Constraints:
1<= |S| <=1000
1<= |oldW|, |newW| <=|S|

to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.

Replace a word

Output Window