Given two strings str1 and str2 each of length N, the task is to generate and print all possible strings of length N such that the character at index i of the generated string is either str1[i] or str2[i]
Input: str1 = “abc”, str2 = “def”
Input: str1 = “a”, str2 = “b”
Approach: The problem can be solved using recursion and at each recursive call, we need to select either the character at str1[i] or the character at str2[i] and append it to the resultant string. The termination condition will be when the length of the resultant string becomes equal to the length of the given strings.
Below is the implementation of the above approach:
abc abf aec aef dbc dbf dec def
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check if it is possible to reach to the index with value K when start index is given
- Minimum digits to be removed to make either all digits or alternating digits same
- Generate all possible sorted arrays from alternate elements of two given sorted arrays
- Program to generate all possible valid IP addresses from given string
- Program to generate all possible valid IP addresses from given string | Set 2
- Generate all possible permutations of a Number divisible by N
- Generate all binary strings from given pattern
- Generate all binary strings without consecutive 1's
- Generate all the binary strings of N bits
- Generate all binary strings of length n with sub-string "01" appearing exactly twice
- Generate a string which differs by only a single character from all given strings
- Print all Strings from array A having all strings from array B as subsequence
- Count of distinct Strings possible by swapping prefixes of pairs of Strings from the Array
- Count of words whose i-th letter is either (i-1)-th, i-th, or (i+1)-th letter of given word
- Find weight of MST in a complete graph with edge-weights either 0 or 1
- Distinct strings such that they contains given strings as sub-sequences
- Generate a string with maximum possible alphabets with odd frequencies
- Maximum length palindromic substring such that it starts and ends with given char
- Generate two output strings depending upon occurrence of character in input string.
- Generate two output strings depending upon occurrence of character in input string in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.