Replace two substrings (of a string) with each other
Last Updated :
15 Sep, 2022
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”.
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++
#include<bits/stdc++.h>
using namespace std;
string updateString(string S,
string A, string B)
{
int l = A.length();
for ( int i = 0; i + l <= S.length(); i++)
{
string curr = S.substr(i, i + l);
if (curr == A)
{
string new_string = "" ;
new_string += S.substr(0, i) + B +
S.substr(i + l, S.length());
S = new_string;
i += l - 1;
}
else if (curr == B)
{
string new_string = "" ;
new_string += S.substr(0, i) + A +
S.substr(i + l, S.length());
S = new_string;
i += l - 1;
}
else
{
}
}
return S;
}
int main()
{
string S = "aaxb" ;
string A = "aa" ;
string B = "bb" ;
cout << (updateString(S, A, B)) << endl;
}
|
Java
class GFG {
static String updateString(String S, String A, String B)
{
int l = A.length();
for ( int i = 0 ; i + l <= S.length(); i++) {
String curr = S.substring(i, i + l);
if (curr.equals(A)) {
String new_string
= S.substring( 0 , i)
+ B + S.substring(i + l, S.length());
S = new_string;
i += l - 1 ;
}
else {
String new_string
= S.substring( 0 , i)
+ A + S.substring(i + l, S.length());
S = new_string;
i += l - 1 ;
}
}
return S;
}
public static void main(String[] args)
{
String S = "aab" ;
String A = "aa" ;
String B = "bb" ;
System.out.println(updateString(S, A, B));
}
}
|
Python3
def updateString(S, A, B):
l = len (A)
i = 0
while i + l < = len (S):
curr = S[i:i + l]
if curr = = A:
new_string = S[ 0 :i] + B + S[i + l: len (S)]
S = new_string
i + = l - 1
else :
new_string = S[ 0 :i] + A + S[i + l: len (S)]
S = new_string
i + = l - 1
i + = 1
return S
if __name__ = = "__main__" :
S = "aab"
A = "aa"
B = "bb"
print (updateString(S, A, B))
|
C#
using System;
class GFG
{
static string updateString( string S, string A, string B)
{
int l = A.Length;
for ( int i = 0; i + l <= S.Length; i++)
{
string curr = S.Substring(i, l);
if (curr.Equals(A))
{
string new_string = S.Substring(0, i) +
B + S.Substring(i + l);
S = new_string;
i += l - 1;
}
else
{
string new_string = S.Substring(0, i) +
A + S.Substring(i + l);
S = new_string;
i += l - 1;
}
}
return S;
}
public static void Main()
{
string S = "aab" ;
string A = "aa" ;
string B = "bb" ;
Console.WriteLine(updateString(S, A, B));
}
}
|
PHP
<?php
function updateString( $S , $A , $B )
{
$l = strlen ( $A );
for ( $i = 0; $i + $l <= strlen ( $S ); $i ++)
{
$curr = substr ( $S , $i , $i + $l );
if ( strcmp ( $curr , $A ) == 0)
{
$new_string = substr ( $S , 0, $i ) . $B .
substr ( $S , $i + $l , strlen ( $S ));
$S = $new_string ;
$i += $l - 1;
}
else
{
$new_string = substr ( $S , 0, $i ) . $A .
substr ( $S , $i + $l , strlen ( $S ));
$S = $new_string ;
$i += $l - 1;
}
}
return $S ;
}
$S = "aab" ;
$A = "aa" ;
$B = "bb" ;
echo (updateString( $S , $A , $B ));
|
Javascript
<script>
function updateString(S, A, B)
{
let l = A.length;
for (let i = 0; i + l <= S.length; i++)
{
let curr = S.substring(i, i + l);
if (curr == A)
{
let new_string = S.substring(0, i) +
B + S.substring(i + l);
S = new_string;
i += l - 1;
}
else
{
let new_string = S.substring(0, i) +
A + S.substring(i + l);
S = new_string;
i += l - 1;
}
}
return S;
}
let S = "aab" ;
let A = "aa" ;
let B = "bb" ;
document.write(updateString(S, A, B));
</script>
|
Complexity Analysis:
- Time Complexity: O(n*n), as substr function is being used inside the loop
- Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...