Skip to content
Related Articles

Related Articles

Generate all possible strings formed by replacing letters with given respective symbols

View Discussion
Improve Article
Save Article
  • Last Updated : 23 Jul, 2021
View Discussion
Improve Article
Save Article

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




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to generate all possible
// string by replacing the characters
// with mapped symbols
void generateLetters(string S, int P,
                     unordered_map<char, char> M)
{
    // Base Case
    if (P == S.size()) {
        cout << S << "\n";
        return;
    }
 
    // Function call with the P-th
    // character not replaced
    generateLetters(S, P + 1, M);
 
    // Replace the P-th character
    S[P] = M[S[P]];
 
    // Function call with the P-th
    // character replaced
    generateLetters(S, P + 1, M);
 
    return;
}
 
// Driver Code
int main()
{
    string S = "aBc";
    unordered_map<char, char> M;
    M['a'] = '$';
    M['B'] = '#';
    M['c'] = '^';
    M['d'] = '&';
    M['1'] = '*';
    M['2'] = '!';
    M['E'] = '@';
 
    // Function Call
    generateLetters(S, 0, M);
 
    return 0;
}

Java




// Java program for the above approach
import java.util.HashMap;
 
class GFG{
 
// Function to generate all possible
// string by replacing the characters
// with mapped symbols
public static void generateLetters(String S, int P, HashMap<Character, Character> M)
{
    // Base Case
    if (P == S.length()) {
        System.out.println(S);
        return;
    }
 
    // Function call with the P-th
    // character not replaced
    generateLetters(S, P + 1, M);
 
    // Replace the P-th character
    S = S.substring(0, P) + M.get(S.charAt(P)) + S.substring(P + 1);
 
    // Function call with the P-th
    // character replaced
    generateLetters(S, P + 1, M);
 
    return;
}
 
// Driver Code
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', '@');
 
    // Function Call
    generateLetters(S, 0, M);
 
}
 
}
 
// This code is contributed by _saurabh_jaiswal.

Python3




# Python program for the above approach
 
# Function to generate all possible
# string by replacing the characters
# with mapped symbols
def generateLetters(S, P, M):
 
    # Base Case
    if (P == len(S)):
        print(S);
        return
 
    # Function call with the P-th
    # character not replaced
    generateLetters(S, P + 1, M);
 
    # Replace the P-th character
    S = S.replace(S[P], M[S[P]])
 
    # Function call with the P-th
    # character replaced
    generateLetters(S, P + 1, M);
 
# Driver Code
S = "aBc";
M = {};
M['a'] = '$'
M['B'] = '#'
M['c'] = '^'
M['d'] = '&'
M['1'] = '*'
M['2'] = '!'
M['E'] = '@'
 
# Function Call
generateLetters(S, 0, M);
 
# This code is contributed by _saurabh_jaiswal.

C#




// C# program for the above approach
 
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to generate all possible
// string by replacing the characters
// with mapped symbols
static void generateLetters(string S, int P,
                     Dictionary<char, char> M)
{
    // Base Case
    if (P == S.Length) {
        Console.WriteLine(S);
        return;
    }
 
    // Function call with the P-th
    // character not replaced
    generateLetters(S, P + 1, M);
 
    // Replace the P-th character
   S = S.Substring(0, P) + M[S[P]] + S.Substring(P + 1);
 
    // Function call with the P-th
    // character replaced
    generateLetters(S, P + 1, M);
 
    return;
}
 
// Driver Code
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','@');
 
    // Function Call
    generateLetters(S, 0, M);
 
}
}
 
// This code is contributed by SURENDRA_GANGWAR.

Javascript




<script>
 
// JavaScript program for the above approach
 
// Function to generate all possible
// string by replacing the characters
// with mapped symbols
function generateLetters(S, P, M)
{
 
    // Base Case
    if (P == S.length)
    {
        document.write(S + "<br>");
        return;
    }
 
    // Function call with the P-th
    // character not replaced
    generateLetters(S, P + 1, M);
 
    // Replace the P-th character
    S = S.replace(S.charAt(P), M.get(S[P]))
 
    // Function call with the P-th
    // character replaced
    generateLetters(S, P + 1, M);
    return;
}
 
// Driver Code
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', '@');
 
// Function Call
generateLetters(S, 0, M);
 
// This code is contributed by Potta Lokesh
 
</script>

Output: 

aBc
aB^
a#c
a#^
$Bc
$B^
$#c
$#^

 

Time Complexity: O(N*2N)
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!