通过将数字替换为“x”、“y”或“z”,可以按字典顺序查找所有字符串
给定一个字符串str,由小写英文字母和数字 (0-9) 组成,任务是按字典顺序打印所有可能的字符串,这些字符串可以通过将每个出现的数字替换为“ x ”、“ y ”来形成或' z '。
例子:
Input: str = “a1b2”
Output: axbx axby axbz aybx ayby aybz azbx azby azbz
Explanation: These string are the 9 possible strings printed in lexicographic order which can be formed from “a1b2” by replacing all digits with either ‘x’, ‘y’ or ‘z’.
Input: str = “abcdef”
Output: abcdef
方法:给定的问题可以在回溯的帮助下使用递归来解决。这个想法是创建一个递归函数,并在迭代给定字符串时,将每个出现的数字替换为“ x ”、“ y ”和“ z ”,并递归调用三个字符串中的每一个的剩余字符串。
下面是上述方法的实现:
C++
// C++ program of the above approach
#include
using namespace std;
// Recursive function to print all
// the possible strings after replacing
// the digits, in lexicographic order
void allPossibleStrings(
string str, string cur, int i)
{
// If the complete string
// has been traversed
if (str.size() == cur.size()) {
// Print current string
cout << cur << " ";
return;
}
// If current character
// is a digit
if (isdigit(str[i])) {
// Recursive call after replacing
// current character with x
allPossibleStrings(
str, cur + "x", i + 1);
// Recursive call after replacing
// current character with y
allPossibleStrings(
str, cur + "y", i + 1);
// Recursive call after replacing
// current character with z
allPossibleStrings(
str, cur + "z", i + 1);
}
else {
// Recursive call after appending
// the current character
allPossibleStrings(
str, cur + str[i], i + 1);
}
}
// Driver Code
int main()
{
string str = "a1b2";
allPossibleStrings(str, "", 0);
return 0;
}
Java
// Java program of the above approach
class GFG {
// Recursive function to print all
// the possible Strings after replacing
// the digits, in lexicographic order
static void allPossibleStrings(
String str, String cur, int i) {
// If the complete String
// has been traversed
if (str.length() == cur.length()) {
// Print current String
System.out.print(cur + " ");
return;
}
// If current character
// is a digit
if (Character.isDigit(str.charAt(i))) {
// Recursive call after replacing
// current character with x
allPossibleStrings(
str, cur + "x", i + 1);
// Recursive call after replacing
// current character with y
allPossibleStrings(
str, cur + "y", i + 1);
// Recursive call after replacing
// current character with z
allPossibleStrings(
str, cur + "z", i + 1);
} else {
// Recursive call after appending
// the current character
allPossibleStrings(
str, cur + str.charAt(i), i + 1);
}
}
// Driver Code
public static void main(String args[]) {
String str = "a1b2";
allPossibleStrings(str, "", 0);
}
}
// This code is contributed by Saurabh Jaiswal
Python3
# python3 program of the above approach
# Recursive function to print all
# the possible strings after replacing
# the digits, in lexicographic order
def allPossibleStrings(str, cur, i):
# If the complete string
# has been traversed
if (len(str) == len(cur)):
# Print current string
print(cur, end=" ")
return
# If current character
# is a digit
if (str[i] >= '0' and str[i] <= '9'):
# Recursive call after replacing
# current character with x
allPossibleStrings(str, cur + "x", i + 1)
# Recursive call after replacing
# current character with y
allPossibleStrings(str, cur + "y", i + 1)
# Recursive call after replacing
# current character with z
allPossibleStrings(str, cur + "z", i + 1)
else:
# Recursive call after appending
# the current character
allPossibleStrings(str, cur + str[i], i + 1)
# Driver Code
if __name__ == "__main__":
str = "a1b2"
allPossibleStrings(str, "", 0)
# This code is contributed by rakeshsahni
C#
// C# program of the above approach
using System;
class GFG
{
// Recursive function to print all
// the possible strings after replacing
// the digits, in lexicographic order
static void allPossibleStrings(
string str, string cur, int i)
{
// If the complete string
// has been traversed
if (str.Length == cur.Length) {
// Print current string
Console.Write(cur + " ");
return;
}
// If current character
// is a digit
if (Char.IsDigit(str[i])) {
// Recursive call after replacing
// current character with x
allPossibleStrings(
str, cur + "x", i + 1);
// Recursive call after replacing
// current character with y
allPossibleStrings(
str, cur + "y", i + 1);
// Recursive call after replacing
// current character with z
allPossibleStrings(
str, cur + "z", i + 1);
}
else {
// Recursive call after appending
// the current character
allPossibleStrings(
str, cur + str[i], i + 1);
}
}
// Driver Code
public static void Main()
{
string str = "a1b2";
allPossibleStrings(str, "", 0);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
axbx axby axbz aybx ayby aybz azbx azby azbz
时间复杂度: O(3 N )
辅助空间: O(1)