Modify string by replacing all occurrences of given characters by specified replacing characters
Last Updated :
24 Jun, 2021
Given a string S consisting of N lowercase alphabets and an array of pairs of characters P[][2], the task is to modify the given string S by replacing all occurrences of character P[i][0] with character P[i][1].
Examples:
Input: S = “aabbgg”, P[][2] = {{a, b}, {b, g}, {g, a}}
Output: bbggaa
Explanation:
Replace ‘a’ by ‘b’ in the original string. Now the string S modifies to “bbbbgg”.
Replace ‘b’ by ‘g’ in the original string. Now the string S modifies to “bbgggg”.
Replace ‘g’ by ‘a’ in the original string. Now the string S modifies to “bbggaa”.
Input: S = “abc”, P[][2] = {{a, b}}
Output: bbc
Naive Approach: The simplest approach to solve the given problem is to create a copy of the original string S, and then for each pair (a, b) traverse the string, and if the character ‘a’ is found then replace it by character ‘b’ in the copy of the original string. After checking for all the pairs, print the modified string S.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void replaceCharacters(
string s, vector<vector< char > > p)
{
int n = s.size(), k = p.size();
string temp = s;
for ( int j = 0; j < k; j++) {
char a = p[j][0], b = p[j][1];
for ( int i = 0; i < n; i++) {
if (s[i] == a) {
temp[i] = b;
}
}
}
cout << temp;
}
int main()
{
string S = "aabbgg" ;
vector<vector< char > > P{ { 'a' , 'b' },
{ 'b' , 'g' },
{ 'g' , 'a' } };
replaceCharacters(S, P);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static void replaceCharacters(String s, char p[][])
{
int n = s.length(), k = p.length;
char temp[] = s.toCharArray();
for ( int j = 0 ; j < k; j++)
{
char a = p[j][ 0 ], b = p[j][ 1 ];
for ( int i = 0 ; i < n; i++)
{
if (s.charAt(i) == a)
{
temp[i] = b;
}
}
}
System.out.println( new String(temp));
}
public static void main(String[] args)
{
String S = "aabbgg" ;
char P[][] = { { 'a' , 'b' },
{ 'b' , 'g' },
{ 'g' , 'a' } };
replaceCharacters(S, P);
}
}
|
Python3
def replaceCharacters(s, p):
n = len (s)
k = len (p)
temp = s
for j in range (k):
a = p[j][ 0 ]
b = p[j][ 1 ]
for i in range (n):
if (s[i] = = a):
temp = list (temp)
temp[i] = b
temp = ''.join(temp)
print (temp)
if __name__ = = '__main__' :
S = "aabbgg"
P = [ [ 'a' , 'b' ],
[ 'b' , 'g' ],
[ 'g' , 'a' ] ]
replaceCharacters(S, P)
|
C#
using System;
class GFG{
static void replaceCharacters( string s, char [,] p)
{
int n = s.Length, k = p.GetLength(0);
char [] temp = s.ToCharArray();
for ( int j = 0; j < k; j++)
{
char a = p[j, 0], b = p[j, 1];
for ( int i = 0; i < n; i++)
{
if (s[i] == a)
{
temp[i] = b;
}
}
}
Console.WriteLine( new string (temp));
}
public static void Main( string [] args)
{
string S = "aabbgg" ;
char [,]P = { { 'a' , 'b' },
{ 'b' , 'g' },
{ 'g' , 'a' } };
replaceCharacters(S, P);
}
}
|
Javascript
<script>
function replaceCharacters(s, p)
{
var n = s.length, k = p.length;
var temp = s;
for (j = 0; j < k; j++)
{
var a = p[j][0], b = p[j][1];
for (i = 0; i < n; i++)
{
if (s.charAt(i) == a)
{
temp[i] = b;
temp = temp.substring(0, i) + b +
temp.substring(i + 1, n);
}
}
}
document.write((temp));
}
var S = "aabbgg" ;
var P = [ [ 'a' , 'b' ],
[ 'b' , 'g' ],
[ 'g' , 'a' ] ];
replaceCharacters(S, P);
</script>
|
Time Complexity: O(K * N)
Auxiliary Space: O(N)
Efficient Approach: The above approach can be optimized by using the two auxiliary arrays of size 26 to store the replacements in the array. Follow the steps below to solve the problem:
- Initialize two arrays, arr[] and brr[] of size 26, and store the characters of the string, S in both the arrays.
- Traverse the array of pairs P using the variable i and perform the following steps:
- Initialize A as P[i][0] and B as P[i][1] denoting character A to be replaced by character B.
- Iterate over the range [0, 25] using the variable j and if arr[j] is equal to A, then update brr[j] to B.
- Traverse the given string S and for each S[i] update it to brr[S[i] – ‘a’].
- After completing the above steps, print the modified string S.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void replaceCharacters(
string s, vector<vector< char > > p)
{
int n = s.size(), k = p.size();
char arr[26];
char brr[26];
for ( int i = 0; i < n; i++) {
arr[s[i] - 'a' ] = s[i];
brr[s[i] - 'a' ] = s[i];
}
for ( int j = 0; j < k; j++) {
char a = p[j][0], b = p[j][1];
for ( int i = 0; i < 26; i++) {
if (arr[i] == a) {
brr[i] = b;
}
}
}
for ( int i = 0; i < n; i++) {
cout << brr[s[i] - 'a' ];
}
}
int main()
{
string S = "aabbgg" ;
vector<vector< char > > P{ { 'a' , 'b' },
{ 'b' , 'g' },
{ 'g' , 'a' } };
replaceCharacters(S, P);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void replaceCharacters(String s, char [][] p)
{
int n = s.length(), k = p.length;
char [] arr = new char [ 26 ];
char [] brr = new char [ 26 ];
for ( int i = 0 ; i < n; i++)
{
arr[s.charAt(i) - 'a' ] = s.charAt(i);
brr[s.charAt(i) - 'a' ] = s.charAt(i);
}
for ( int j = 0 ; j < k; j++)
{
char a = p[j][ 0 ], b = p[j][ 1 ];
for ( int i = 0 ; i < 26 ; i++)
{
if (arr[i] == a)
{
brr[i] = b;
}
}
}
for ( int i = 0 ; i < n; i++)
{
System.out.print(brr[s.charAt(i) - 'a' ]);
}
}
public static void main(String[] args)
{
String S = "aabbgg" ;
char [][] P = { { 'a' , 'b' },
{ 'b' , 'g' },
{ 'g' , 'a' } };
replaceCharacters(S, P);
}
}
|
Python3
def replaceCharacters(s, p):
n, k = len (s), len (p)
arr = [ 0 ] * 26
brr = [ 0 ] * 26
for i in range (n):
arr[ ord (s[i]) - ord ( 'a' )] = s[i]
brr[ ord (s[i]) - ord ( 'a' )] = s[i]
for j in range (k):
a, b = p[j][ 0 ], p[j][ 1 ]
for i in range ( 26 ):
if (arr[i] = = a):
brr[i] = b
for i in range (n):
print (brr[ ord (s[i]) - ord ( 'a' )], end = "")
if __name__ = = '__main__' :
S = "aabbgg"
P = [ [ 'a' , 'b' ],
[ 'b' , 'g' ],
[ 'g' , 'a' ] ]
replaceCharacters(S, P)
|
C#
using System;
public class GFG{
static void replaceCharacters( string s, char [,] p)
{
int n = s.Length, k = p.GetLength(0);
char [] arr = new char [26];
char [] brr = new char [26];
for ( int i = 0; i < n; i++)
{
arr[s[i] - 'a' ] = s[i];
brr[s[i] - 'a' ] = s[i];
}
for ( int j = 0; j < k; j++)
{
char a = p[j,0], b = p[j,1];
for ( int i = 0; i < 26; i++)
{
if (arr[i] == a)
{
brr[i] = b;
}
}
}
for ( int i = 0; i < n; i++)
{
Console.Write(brr[s[i] - 'a' ]);
}
}
static public void Main ()
{
String S = "aabbgg" ;
char [,] P = { { 'a' , 'b' },
{ 'b' , 'g' },
{ 'g' , 'a' } };
replaceCharacters(S, P);
}
}
|
Javascript
<script>
function replaceCharacters(s, p) {
var n = s.length,
k = p.length;
var arr = new Array(26).fill(0);
var brr = new Array(26).fill(0);
for ( var i = 0; i < n; i++) {
arr[s[i].charCodeAt(0) - "a" .charCodeAt(0)] = s[i];
brr[s[i].charCodeAt(0) - "a" .charCodeAt(0)] = s[i];
}
for ( var j = 0; j < k; j++) {
var a = p[j][0],
b = p[j][1];
for ( var i = 0; i < 26; i++) {
if (arr[i] === a) {
brr[i] = b;
}
}
}
for ( var i = 0; i < n; i++) {
document.write(brr[s[i].charCodeAt(0) -
"a" .charCodeAt(0)]);
}
}
var S = "aabbgg" ;
var P = [
[ "a" , "b" ],
[ "b" , "g" ],
[ "g" , "a" ],
];
replaceCharacters(S, P);
</script>
|
Time Complexity: O(N + K)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...