Generate all possible strings formed by replacing letters with given respective symbols
Last Updated :
23 Jul, 2021
Given a string S consisting of N characters and an array M[] of pairs of characters such that any character M[i][0] can be replaced with the character M[i][1] in the string S, the task is to generate all the possible strings formed by replacing some characters of the string with their respective symbols in the array M[].
Examples:
Input: S = “aBc”, M = {‘a’ : ‘$’, ‘B’ : ‘#’, ‘c’ : ‘^’}
Output:
aBc
aB^
a#c
a#^
$Bc
$B^
$#c
$#^
Input: S = “a”, M={‘a’ : ‘$’}
Output:
a
$
Approach: The given problem can be solved by using Backtracking to generate all possible strings by replacing each character with the mapped character in the array M[]. Follow the steps below to solve the problem:
- Store all the pairs of the mapped characters in the array M[] in a map, say Map.
- Define a recursive function say generateLetters(S, P), where S is the modified string and P is the index of the current character:
- Check for the base case i.e., if index P equals to the N then print the string S and return.
- Do not change the current character and recursively call for the function, generateLetters(S, P + 1).
- Now, replace the character, S[P] with the respective symbol in the map M and call for the function, generateLetters(S, P+1).
- After completing the above steps, call for the function generateLetters(S, 0) to print all possible strings.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void generateLetters(string S, int P,
unordered_map< char , char > M)
{
if (P == S.size()) {
cout << S << "\n" ;
return ;
}
generateLetters(S, P + 1, M);
S[P] = M[S[P]];
generateLetters(S, P + 1, M);
return ;
}
int main()
{
string S = "aBc" ;
unordered_map< char , char > M;
M[ 'a' ] = '$' ;
M[ 'B' ] = '#' ;
M[ 'c' ] = '^' ;
M[ 'd' ] = '&' ;
M[ '1' ] = '*' ;
M[ '2' ] = '!' ;
M[ 'E' ] = '@' ;
generateLetters(S, 0, M);
return 0;
}
|
Java
import java.util.HashMap;
class GFG{
public static void generateLetters(String S, int P, HashMap<Character, Character> M)
{
if (P == S.length()) {
System.out.println(S);
return ;
}
generateLetters(S, P + 1 , M);
S = S.substring( 0 , P) + M.get(S.charAt(P)) + S.substring(P + 1 );
generateLetters(S, P + 1 , M);
return ;
}
public static void main(String args[])
{
String S = "aBc" ;
HashMap<Character, Character> M = new HashMap<Character, Character>();
M.put( 'a' , '$' );
M.put( 'B' , '#' );
M.put( 'c' , '^' );
M.put( 'd' , '&' );
M.put( '1' , '*' );
M.put( '2' , '!' );
M.put( 'E' , '@' );
generateLetters(S, 0 , M);
}
}
|
Python3
def generateLetters(S, P, M):
if (P = = len (S)):
print (S);
return
generateLetters(S, P + 1 , M);
S = S.replace(S[P], M[S[P]])
generateLetters(S, P + 1 , M);
S = "aBc" ;
M = {};
M[ 'a' ] = '$'
M[ 'B' ] = '#'
M[ 'c' ] = '^'
M[ 'd' ] = '&'
M[ '1' ] = '*'
M[ '2' ] = '!'
M[ 'E' ] = '@'
generateLetters(S, 0 , M);
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void generateLetters( string S, int P,
Dictionary< char , char > M)
{
if (P == S.Length) {
Console.WriteLine(S);
return ;
}
generateLetters(S, P + 1, M);
S = S.Substring(0, P) + M[S[P]] + S.Substring(P + 1);
generateLetters(S, P + 1, M);
return ;
}
public static void Main()
{
string S = "aBc" ;
Dictionary< char , char > M = new Dictionary< char , char >();
M.Add( 'a' , '$' );
M.Add( 'B' , '#' );
M.Add( 'c' , '^' );
M.Add( 'd' , '&' );
M.Add( '1' , '*' );
M.Add( '2' , '!' );
M.Add( 'E' , '@' );
generateLetters(S, 0, M);
}
}
|
Javascript
<script>
function generateLetters(S, P, M)
{
if (P == S.length)
{
document.write(S + "<br>" );
return ;
}
generateLetters(S, P + 1, M);
S = S.replace(S.charAt(P), M.get(S[P]))
generateLetters(S, P + 1, M);
return ;
}
let S = "aBc" ;
let M = new Map();
M.set( 'a' , '$' );
M.set( 'B' , '#' );
M.set( 'c' , '^' );
M.set( 'd' , '&' );
M.set( '1' , '*' );
M.set( '2' , '!' );
M.set( 'E' , '@' );
generateLetters(S, 0, M);
</script>
|
Output:
aBc
aB^
a#c
a#^
$Bc
$B^
$#c
$#^
Time Complexity: O(N*2N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...