# Replace two substrings (of a string) with each other

Given 3 strings S, A and B. The task is to replace every sub-string of S equal to A with B and every sub-string of S equal to B with A. It is possible that two or more sub-strings matching A or B overlap. To avoid confusion about this situation, you should find the leftmost sub-string that matches A or B, replace it, and then continue with the rest of the string.
For example, when matching A = “aa” with S = “aaa”, A[0, 1] will be given preference over A[1, 2].
Note that A and B will have the same length and A != B.

Examples:

Input: S = “aab”, A = “aa”, B = “bb”
Output: bbb
We match the first two characters with A and replacing it with B we get bbb.
Then we continue the algorithm starting at index 3 and we don’t find any more matches.

Input: S = “aabbaabb”, A = “aa”, B = “bb”
Output: bbaabbaa
We replace all the occurrences of “aa” with “bb” and “bb” with “aa”, so the resultant string is “bbaabbaa”.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Go through every possible sub-string from S of length len(A). if any sub-string matches A or B then update the string as required and print the updated string in the end.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the resultant string ` `string updateString(string S,  ` `                    ``string A, string B) ` `{ ` ` `  `    ``int` `l = A.length(); ` ` `  `    ``// Iterate through all positions i ` `    ``for` `(``int` `i = 0; i + l <= S.length(); i++) ` `    ``{ ` ` `  `        ``// Current sub-string of  ` `        ``// length = len(A) = len(B) ` `        ``string curr = S.substr(i, i + l); ` ` `  `        ``// If current sub-string gets  ` `        ``// equal to A or B ` `        ``if` `(curr == A)  ` `        ``{ ` ` `  `            ``// Update S after replacing A ` `            ``string new_string = ``""``; ` `            ``new_string += S.substr(0, i) + B +  ` `                          ``S.substr(i + l, S.length()); ` `            ``S = new_string; ` `            ``i += l - 1; ` `        ``} ` `        ``else` `        ``{ ` ` `  `            ``// Update S after replacing B ` `            ``string new_string = ``""``; ` `            ``new_string += S.substr(0, i) + A +  ` `                          ``S.substr(i + l, S.length()); ` `            ``S = new_string; ` `            ``i += l - 1; ` `        ``} ` `    ``} ` ` `  `    ``// Return the updated string ` `    ``return` `S; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``string S = ``"aab"``; ` `    ``string A = ``"aa"``; ` `    ``string B = ``"bb"``; ` ` `  `    ``cout << (updateString(S, A, B)) << endl; ` `} ` ` `  `// This code is contributed by ` `// Surendra_Gangwar `

## Java

 `// Java implementation of the approach ` `class` `GFG { ` ` `  `    ``// Function to return the resultant string ` `    ``static` `String updateString(String S, String A, String B) ` `    ``{ ` ` `  `        ``int` `l = A.length(); ` ` `  `        ``// Iterate through all positions i ` `        ``for` `(``int` `i = ``0``; i + l <= S.length(); i++) { ` ` `  `            ``// Current sub-string of length = len(A) = len(B) ` `            ``String curr = S.substring(i, i + l); ` ` `  `            ``// If current sub-string gets equal to A or B ` `            ``if` `(curr.equals(A)) { ` ` `  `                ``// Update S after replacing A ` `                ``String new_string ` `                    ``= S.substring(``0``, i) ` `                      ``+ B + S.substring(i + l, S.length()); ` `                ``S = new_string; ` `                ``i += l - ``1``; ` `            ``} ` `            ``else` `{ ` ` `  `                ``// Update S after replacing B ` `                ``String new_string ` `                    ``= S.substring(``0``, i) ` `                      ``+ A + S.substring(i + l, S.length()); ` `                ``S = new_string; ` `                ``i += l - ``1``; ` `            ``} ` `        ``} ` ` `  `        ``// Return the updated string ` `        ``return` `S; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``String S = ``"aab"``; ` `        ``String A = ``"aa"``; ` `        ``String B = ``"bb"``; ` ` `  `        ``System.out.println(updateString(S, A, B)); ` `    ``} ` `} `

## Python3

 `# Python3 implementation of the approach  ` ` `  `# Function to return the resultant string  ` `def` `updateString(S, A, B):  ` `     `  `    ``l ``=` `len``(A)  ` `     `  `    ``# Iterate through all positions i  ` `    ``i ``=` `0` `    ``while` `i ``+` `l <``=` `len``(S):  ` ` `  `        ``# Current sub-string of  ` `        ``# length = len(A) = len(B)  ` `        ``curr ``=` `S[i:i``+``l]  ` ` `  `        ``# If current sub-string gets ` `        ``# equal to A or B  ` `        ``if` `curr ``=``=` `A:  ` ` `  `            ``# Update S after replacing A  ` `            ``new_string ``=` `S[``0``:i] ``+` `B ``+` `S[i ``+` `l:``len``(S)]  ` `            ``S ``=` `new_string  ` `            ``i ``+``=` `l ``-` `1` `             `  `        ``else``: ` ` `  `            ``# Update S after replacing B  ` `            ``new_string ``=` `S[``0``:i] ``+` `A ``+` `S[i ``+` `l:``len``(S)]  ` `            ``S ``=` `new_string  ` `            ``i ``+``=` `l ``-` `1` `         `  `        ``i ``+``=` `1` `     `  `    ``# Return the updated string  ` `    ``return` `S  ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"``: ` `     `  `    ``S ``=` `"aab"` `    ``A ``=` `"aa"` `    ``B ``=` `"bb"` ` `  `    ``print``(updateString(S, A, B))  ` `     `  `# This code is contributed by Rituraj Jain `

## C#

 `// C# implementation of the approach  ` `using` `System; ` ` `  `class` `GFG  ` `{  ` ` `  `    ``// Function to return the resultant string  ` `    ``static` `string` `updateString(``string` `S, ``string` `A, ``string` `B)  ` `    ``{  ` `        ``int` `l = A.Length;  ` ` `  `        ``// Iterate through all positions i  ` `        ``for` `(``int` `i = 0; i + l <= S.Length; i++)  ` `        ``{  ` ` `  `            ``// Current sub-string of length = len(A) = len(B)  ` `            ``string` `curr = S.Substring(i, l);  ` ` `  `            ``// If current sub-string gets equal to A or B  ` `            ``if` `(curr.Equals(A)) ` `            ``{  ` ` `  `                ``// Update S after replacing A  ` `                ``string` `new_string = S.Substring(0, i) + ` `                                 ``B + S.Substring(i + l);  ` `                ``S = new_string;  ` `                ``i += l - 1;  ` `            ``}  ` `            ``else`  `            ``{  ` ` `  `                ``// Update S after replacing B  ` `                ``string` `new_string = S.Substring(0, i) + ` `                                ``A + S.Substring(i + l);  ` `                ``S = new_string;  ` `                ``i += l - 1;  ` `            ``}  ` `        ``}  ` ` `  `        ``// Return the updated string  ` `        ``return` `S;  ` `    ``}  ` ` `  `    ``// Driver code  ` `    ``public` `static` `void` `Main()  ` `    ``{  ` `        ``string` `S = ``"aab"``;  ` `        ``string` `A = ``"aa"``;  ` `        ``string` `B = ``"bb"``;  ` `        ``Console.WriteLine(updateString(S, A, B));  ` `    ``}  ` `}  ` ` `  `// This code is contributed by Ryuga `

## PHP

 `

Output:

```bbb
```

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.