Open In App

Find the resultant String after replacing X with Y and removing Z

Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ program to find the resultant String
// after replacing X with Y and removing Z
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to replace and remove
void replaceRemove(string& s, char X, char Y, char Z)
{
 
    // Two pointer start and end points
    // to beginning and end position in the string
    int start = 0, end = s.size() - 1;
 
    while (start <= end) {
 
        // If start is having Z
        // find X pos in end and
        // replace Z with another character
        if (s[start] == Z) {
 
            // Find location for having
            // different character
            // instead of Z
            while (end >= 0 && s[end] == Z) {
                end--;
            }
 
            // If found swap character
            // at start and end
            if (end > start) {
                swap(s[start], s[end]);
                if (s[start] == X)
                    s[start] = Y;
                start++;
            }
        }
        // Else increment start
        // Also checkin for X
        // at start position
        else {
            if (s[start] == X)
                s[start] = Y;
            start++;
        }
    }
    while (s.size() > 0 && s[s.size() - 1] == Z) {
        s.pop_back();
    }
}
 
// Driver code
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




// Java program to find the resultant String
// after replacing X with Y and removing Z
class GFG
{
     
    // Function to replace and remove
    static String replaceRemove(char []s, char X,
                                   char Y, char Z)
    {
     
        // Two pointer start and end points
        // to beginning and end position in the string
        int start = 0, end = s.length - 1;
     
        while (start <= end)
        {
     
            // If start is having Z
            // find X pos in end and
            // replace Z with another character
            if (s[start] == Z)
            {
     
                // Find location for having
                // different character
                // instead of Z
                while (end >= 0 && s[end] == Z)
                {
                    end--;
                }
     
                char temp ;
                 
                // If found swap character
                // at start and end
                if (end > start)
                {
                    temp = s[start];
                    s[start] = s[end];
                    s[end] = temp;
                     
                    if (s[start] == X)
                        s[start] = Y;
                    start++;
                }
            }
             
            // Else increment start
            // Also checkin for X
            // at start position
            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;
    }
     
    // Driver code
    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);
        }
    }
}
 
// This code is contributed by AnkitRai01


Python3




# Python3 program to find the resultant String
# after replacing X with Y and removing Z
 
# Function to replace and remove
def replaceRemove(s, X, Y, Z) :
 
    s = list(s);
     
    # Two pointer start and end points
    # to beginning and end position in the string
    start = 0;
    end = len(s) - 1;
     
    while (start <= end) :
         
        # If start is having Z
        # find X pos in end and
        # replace Z with another character
        if (s[start] == Z) :
             
            # Find location for having
            # different character
            # instead of Z
            while (end >= 0 and s[end] == Z) :
                end -= 1;
                 
            # If found swap character
            # at start and end
            if (end > start) :
                s[start], s[end] = s[end], s[start]
                if (s[start] == X):
                    s[start] = Y;
                     
                start += 1
                 
        # Else increment start
        # Also checkin for X
        # at start position
        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)
 
# Driver code
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);
 
# This code is contributed by AnkitRai01


C#




// C# program to find the resultant String
// after replacing X with Y and removing Z
using System;
 
class GFG
{
     
    // Function to replace and remove
    static String replaceRemove(char []s, char X,
                                char Y, char Z)
    {
     
        // Two pointer start and end points
        // to beginning and end position in the string
        int start = 0, end = s.Length - 1;
     
        while (start <= end)
        {
     
            // If start is having Z
            // find X pos in end and
            // replace Z with another character
            if (s[start] == Z)
            {
     
                // Find location for having
                // different character
                // instead of Z
                while (end >= 0 && s[end] == Z)
                {
                    end--;
                }
     
                char temp ;
                 
                // If found swap character
                // at start and end
                if (end > start)
                {
                    temp = s[start];
                    s[start] = s[end];
                    s[end] = temp;
                     
                    if (s[start] == X)
                        s[start] = Y;
                    start++;
                }
            }
             
            // Else increment start
            // Also checkin for X
            // at start position
            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;
    }
     
    // Driver code
    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);
        }
    }
}
 
// This code is contributed by PrinciRaj1992


Javascript




// Javascript program to find the resultant String
// after replacing X with Y and removing Z
 
// Function to replace and remove
function replaceRemove(s, X, Y, Z)
{
    // Two pointer start and end points
    // to beginning and end position in the string
    let start = 0;
    let end = s.length - 1;
 
    while (start <= end) {
 
        // If start is having Z
        // find X pos in end and
        // replace Z with another character
        if (s[start] == Z) {
 
            // Find location for having
            // different character
            // instead of Z
            while (end >= 0 && s[end] == Z) {
                end--;
            }
 
            // If found swap character
            // at start and end
            if (end > start)
            {
                temp = s[start];
                s[start] = s[end];
                s[end] = temp;
                 
                if (s[start] == X) {
                    s[start] = Y;
                }
                start++;
            }
        }
             
        // Else increment start
        // Also checkin for X
        // at start position
        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;
}
 
// Driver code
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);
}
 
// This code is contributed by Samim Hossain Mondal.


Output

ndtmd

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.



Last Updated : 30 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads