📌  相关文章
📜  通过用给定的相应符号替换字母生成所有可能的字符串

📅  最后修改于: 2022-05-13 01:56:09.318000             🧑  作者: Mango

通过用给定的相应符号替换字母生成所有可能的字符串

给定一个由N个字符组成的字符串S和一个字符对数组M[] ,使得任何字符M[i][0]都可以替换为字符串S中的字符M[i][1] ,任务是通过用数组M[]中的相应符号替换字符串的某些字符来生成所有可能的字符串。

例子:

方法:给定的问题可以通过使用回溯生成所有可能的字符串来解决,方法是将每个字符替换为数组M[]中的映射字符。请按照以下步骤解决问题:

  • 将数组M[]中的所有映射字符对存储在映射中,例如Map
  • 定义一个递归函数generateLetters(S, P) ,其中S是修改后的字符串, P是当前字符的索引:
    • 检查基本情况,即如果索引P等于N则打印字符串S并返回。
    • 不要更改当前字符并递归调用函数generateLetters(S, P + 1)
    • 现在,用映射M中的相应符号替换字符S[P]并调用函数generateLetters(S, P+1)
  • 完成上述步骤后,调用函数generateLetters(S, 0)打印所有可能的字符串。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to generate all possible
// string by replacing the characters
// with mapped symbols
void generateLetters(string S, int P,
                     unordered_map 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 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 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 M = new HashMap();
    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 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 M = new Dictionary();
    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


输出:
aBc
aB^
a#c
a#^
$Bc
$B^
$#c
$#^

时间复杂度: O(N*2 N )
辅助空间: O(1)