Given a string str that may contain one more occurrences of “AB”. Replace all occurrences of “AB” with “C” in str.
Examples:
Input : str = "helloABworld"
Output : str = "helloCworld"
Input : str = "fghABsdfABysu"
Output : str = "fghCsdfCysu"
A simple solution is to find all occurrences of “AB”. For every occurrence, replace it with C and move all characters one position back.
Implementation:
C++
#include <bits/stdc++.h>
void translate( char * str)
{
if (str[0] == '\0' )
return ;
for ( int i=1; str[i] != '\0' ; i++)
{
if (str[i-1]== 'A' && str[i]== 'B' )
{
str[i-1] = 'C' ;
for ( int j=i; str[j]!= '\0' ; j++)
str[j] = str[j+1];
}
}
return ;
}
int main()
{
char str[] = "helloABworldABGfG" ;
translate(str);
printf ( "The modified string is :\n" );
printf ( "%s" , str);
}
|
Java
import java.io.*;
class GFG {
static void translate( char str[])
{
for ( int i = 1 ; i < str.length; i++)
{
if (str[i - 1 ] == 'A' && str[i] == 'B' )
{
str[i - 1 ] = 'C' ;
int j;
for (j = i; j < str.length - 1 ; j++)
str[j] = str[j + 1 ];
str[j] = ' ' ;
}
}
return ;
}
public static void main(String args[])
{
String st = "helloABworldABGfG" ;
char str[] = st.toCharArray();
translate(str);
System.out.println( "The modified string is :" );
System.out.println(str);
}
}
|
Python3
def translate(st) :
for i in range ( 1 , len (st)) :
if (st[i - 1 ] = = 'A' and st[i] = = 'B' ) :
st[i - 1 ] = 'C'
for j in range (i, len (st) - 1 ) :
st[j] = st[j + 1 ]
st[ len (st) - 1 ] = ' '
return
st = list ( "helloABworldABGfG" )
translate(st)
print ( "The modified string is :" )
print (''.join(st))
|
C#
using System;
class GFG {
static void translate( char []str)
{
for ( int i = 1; i < str.Length; i++)
{
if (str[i - 1] == 'A' && str[i] == 'B' )
{
str[i - 1] = 'C' ;
int j;
for (j = i; j < str.Length - 1; j++)
str[j] = str[j + 1];
str[j] = ' ' ;
}
}
return ;
}
public static void Main()
{
String st = "helloABworldABGfG" ;
char []str = st.ToCharArray();
translate(str);
Console.WriteLine( "The modified string is :" );
Console.Write(str);
}
}
|
PHP
<?php
function translate(& $str )
{
if ( $str [0] == '' )
return ;
for ( $i = 1; $i < strlen ( $str ); $i ++)
{
if ( $str [ $i - 1] == 'A' && $str [ $i ] == 'B' )
{
$str [ $i - 1] = 'C' ;
for ( $j = $i ; $j < strlen ( $str ) ; $j ++)
$str [ $j ] = $str [ $j + 1];
}
}
return ;
}
$str = "helloABworldABGfG" ;
translate( $str );
echo "The modified string is :\n" ;
echo $str ;
?>
|
Javascript
<script>
function translate(str)
{
for (let i = 1; i < str.length; i++)
{
if (str[i - 1] == 'A ' && str[i] == ' B ')
{
// Replace previous character with
// ' C ' and move all subsequent
// characters one position back
str[i - 1] = ' C ';
let j;
for(j = i; j < str.length - 1; j++)
str[j] = str[j + 1];
str[j] = ' ';
}
}
return ;
}
let st = "helloABworldABGfG ";
let str = st.split(" ");
translate(str);
document.write(" The modified string is :<br> ");
document.write(str.join(" "));
</script>
|
Output :
The modified string is :
helloCworldCGfG
Time Complexity : O(n2)
Auxiliary Space : O(1)
An efficient solution is to keep track of two indexes, one for modified string (i in below code) and other for original string (j in below code). If we find “AB” at current index j, we increment j by 2 and i by 1. Otherwise, we increment both and copy character from j to i.
Below is implementation of above idea.
C++
#include <bits/stdc++.h>
using namespace std;
void translate(string &str)
{
int len = str.size();
if (len < 2)
return ;
int i = 0;
int j = 0;
while (j < len - 1) {
if (str[j] == 'A' && str[j + 1] == 'B' ) {
j = j + 2;
str[i++] = 'C' ;
continue ;
}
str[i++] = str[j++];
}
if (j == len - 1)
str[i++] = str[j];
str[i] = ' ' ;
str[len - 1] = ' ' ;
}
int main()
{
string str = "helloABworldABGfG" ;
translate(str);
cout << "The modified string is:" << endl << str;
}
|
Java
import java.io.*;
class GFG {
static void translate( char str[])
{
int len = str.length;
if (len < 2 )
return ;
int i = 0 ;
int j = 0 ;
while (j < len - 1 )
{
if (str[j] == 'A' && str[j + 1 ] == 'B' )
{
j = j + 2 ;
str[i++] = 'C' ;
continue ;
}
str[i++] = str[j++];
}
if (j == len - 1 )
str[i++] = str[j];
str[i] = ' ' ;
str[len - 1 ]= ' ' ;
}
public static void main(String args[])
{
String st= "helloABworldABGfG" ;
char str[] = st.toCharArray();
translate(str);
System.out.println( "The modified string is :" );
System.out.println(str);
}
}
|
Python3
def translate(st) :
l = len (st)
if (l < 2 ) :
return
i = 0
j = 0
while (j < l - 1 ) :
if (st[j] = = 'A' and st[j + 1 ] = = 'B' ) :
j + = 2
st[i] = 'C'
i + = 1
continue
st[i] = st[j]
i + = 1
j + = 1
if (j = = l - 1 ) :
st[i] = st[j]
i + = 1
st[i] = ' '
st[l - 1 ] = ' '
st = list ( "helloABworldABGfG" )
translate(st)
print ( "The modified string is :" )
print (''.join(st))
|
C#
using System;
class GFG {
static void translate( char []str)
{
int len = str.Length;
if (len < 2)
return ;
int i = 0;
int j = 0;
while (j < len - 1)
{
if (str[j] == 'A' && str[j + 1] == 'B' )
{
j = j + 2;
str[i++] = 'C' ;
continue ;
}
str[i++] = str[j++];
}
if (j == len - 1)
str[i++] = str[j];
str[i] = ' ' ;
str[len - 1]= ' ' ;
}
public static void Main()
{
String st= "helloABworldABGfG" ;
char []str = st.ToCharArray();
translate(str);
Console.Write( "The modified string is :" );
Console.Write(str);
}
}
|
Javascript
<script>
function translate(str)
{
var len = str.length;
if (len < 2)
return ;
var i = 0;
var j = 0;
while (j < len - 1)
{
if (str[j] == 'A' && str[j + 1] == 'B' )
{
j = j + 2;
let firstPart = str.substr(0, i);
let lastPart = str.substr(i + 1);
str = firstPart + 'C' + lastPart;
i += 1;
continue ;
}
let firstPart = str.substr(0, i);
let lastPart = str.substr(i + 1);
str = firstPart + str[j] + lastPart;
i += 1;
j += 1;
}
if (j == len - 1)
{
let firstPart = str.substr(0, i);
let lastPart = str.substr(i + 1);
str = firstPart + str[j] + lastPart;
i += 1;
}
let firstPart = str.substr(0, i);
let lastPart = str.substr(i + 1);
str = firstPart + ' ' + lastPart;
firstPart = str.substr(0, len - 1);
lastPart = str.substr(len);
str = firstPart + ' ' + lastPart;
return str;
}
var str = "helloABworldABGfG" ;
document.write( "The modified string is :" +
"<br>" + translate(str));
</script>
|
Output
The modified string is:
helloCworldCGfG
Time Complexity : O(n)
Auxiliary Space : O(1)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!