Given a string str, the task is to replace all occurrences of the given X with given Y and also remove any occurrences of the given Z if present in it with no extra space
Examples:
Input: str = “batman”, X = ‘a’, Y = ‘d’, Z = ‘b’
Output: ntdmd
Input: str = “abba”, X = ‘a’, Y = ‘d’, Z = ‘b’
Output: dd
Recommended: Please try your approach on {IDE} first, before moving on to the solution.
Approach:
- The idea is based on the 2 pointers.
- Let two-variable start and end points to the beginning and end of the string.
- Now if the character at the start is Z, replace it with a character not having Y at another pointer pointing to a location > start keeping in mind to replace character X with Y if found.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void replaceRemove(string& s, char X, char Y, char Z)
{
int start = 0, end = s.size() - 1;
while (start <= end) {
if (s[start] == Z) {
while (end >= 0 && s[end] == Z) {
end--;
}
if (end > start) {
swap(s[start], s[end]);
if (s[start] == X)
s[start] = Y;
start++;
}
}
else {
if (s[start] == X)
s[start] = Y;
start++;
}
}
while (s.size() > 0 && s[s.size() - 1] == Z) {
s.pop_back();
}
}
int main()
{
string str = "batman" ;
char X = 'a' , Y = 'd' , Z = 'b' ;
replaceRemove(str, X, Y, Z);
if (str.size() == 0) {
cout << -1;
}
else {
cout << str;
}
return 0;
}
|
Java
class GFG
{
static String replaceRemove( char []s, char X,
char Y, char Z)
{
int start = 0 , end = s.length - 1 ;
while (start <= end)
{
if (s[start] == Z)
{
while (end >= 0 && s[end] == Z)
{
end--;
}
char temp ;
if (end > start)
{
temp = s[start];
s[start] = s[end];
s[end] = temp;
if (s[start] == X)
s[start] = Y;
start++;
}
}
else
{
if (s[start] == X)
s[start] = Y;
start++;
}
}
String new_s = new String(s);
while (new_s.length() > 0 &&
new_s.charAt(new_s.length() - 1 ) == Z)
{
new_s = new_s.substring( 0 ,new_s.length() - 1 );
}
return new_s;
}
public static void main (String[] args)
{
String str = "batman" ;
char X = 'a' , Y = 'd' , Z = 'b' ;
str = replaceRemove(str.toCharArray() , X, Y, Z);
if (str.length() == 0 )
{
System.out.println(- 1 );
}
else
{
System.out.println(str);
}
}
}
|
Python3
def replaceRemove(s, X, Y, Z) :
s = list (s);
start = 0 ;
end = len (s) - 1 ;
while (start < = end) :
if (s[start] = = Z) :
while (end > = 0 and s[end] = = Z) :
end - = 1 ;
if (end > start) :
s[start], s[end] = s[end], s[start]
if (s[start] = = X):
s[start] = Y;
start + = 1
else :
if (s[start] = = X) :
s[start] = Y;
start + = 1 ;
while ( len (s) > 0 and s[ len (s) - 1 ] = = Z):
s.pop();
return "".join(s)
if __name__ = = "__main__" :
string = "batman" ;
X = 'a' ; Y = 'd' ; Z = 'b' ;
string = replaceRemove(string, X, Y, Z);
if ( len (string) = = 0 ) :
print ( - 1 );
else :
print (string);
|
C#
using System;
class GFG
{
static String replaceRemove( char []s, char X,
char Y, char Z)
{
int start = 0, end = s.Length - 1;
while (start <= end)
{
if (s[start] == Z)
{
while (end >= 0 && s[end] == Z)
{
end--;
}
char temp ;
if (end > start)
{
temp = s[start];
s[start] = s[end];
s[end] = temp;
if (s[start] == X)
s[start] = Y;
start++;
}
}
else
{
if (s[start] == X)
s[start] = Y;
start++;
}
}
String new_s = new String(s);
while (new_s.Length > 0 &&
new_s[new_s.Length - 1] == Z)
{
new_s = new_s.Substring(0,new_s.Length - 1);
}
return new_s;
}
public static void Main(String[] args)
{
String str = "batman" ;
char X = 'a' , Y = 'd' , Z = 'b' ;
str = replaceRemove(str.ToCharArray() , X, Y, Z);
if (str.Length == 0)
{
Console.WriteLine(-1);
}
else
{
Console.WriteLine(str);
}
}
}
|
Javascript
function replaceRemove(s, X, Y, Z)
{
let start = 0;
let end = s.length - 1;
while (start <= end) {
if (s[start] == Z) {
while (end >= 0 && s[end] == Z) {
end--;
}
if (end > start)
{
temp = s[start];
s[start] = s[end];
s[end] = temp;
if (s[start] == X) {
s[start] = Y;
}
start++;
}
}
else
{
if (s[start] == X) {
s[start] = Y;
}
start++;
}
}
let str = s.join( "" );
while (str.length > 0 && str[str.length - 1] == Z) {
str = str.substr(0, str.length - 1);
}
return str;
}
let str = "batman" ;
let X = 'a' ;
let Y = 'd' ;
let Z = 'b' ;
let s = Array.from(str);
str = replaceRemove(s, X, Y, Z);
if (s.length == 0) {
console.log(-1);
}
else {
console.log(str);
}
|
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(1), no extra space is required, so it is a constant.