Given the numeric string str, where 1 represents ‘a’, 2 represents ‘b’, …, 26 represents ‘z’, the task is to print all possible alphabetical strings that can be obtained from str.
Input: str = “1123”
The given string can be splitted as:
1) “1123” = “1” + “1” + “2” + “3” = aabc
2) “1123” = “11” + “2” + “3” = kbc
3) “1123” = “1” + “12” + “3” = alc
4) “1123” = “1” + “1” + “23” = aaw
5) “1123” = “11” + “23” = aabc
Input: str = “56”
The given string can be splitted as:
1) “56” = “5” + “6” = ef
Approach: It can be observed that every single character represents an alphabet apart from 0. This problem is recursive and can be broken into sub-problems. The terminating condition will be when the passed string is empty. Below are the steps to solve the problem:
- Create a helper function getChar() that returns the corresponding alphabet of the given numeric character.
- Create a recursive function that takes the input as a string and returns an array of the desired answer of every character extracted.
- The base case is when the input string is empty. Return an array of length one containing an empty string for this case.
- Extract every single character by using the helper function and append it to the empty string first and store it in an array, say output1.
- At the same time check if the length of the characters is greater than or equal to two and also check if the two characters extracted lies in the range of alphabets. Now, store the corresponding character in an array, say output2.
- Then create a final output array whose length will be the sum of the length of output1 and output2 and store their answers and return it.
- Repeat the above steps for every single or pair of adjacent characters extracted. Now, return the final array obtained.
- Traverse the array and for each string, generate corresponding strings of lowercase alphabets and print them.
Below is the implementation of the above approach:
Time Complexity: O(2N)
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.
- Print all possible combinations of the string by replacing '$' with any other digit from the string
- Print the two possible permutations from a given sequence
- Given a sequence of words, print all anagrams together | Set 1
- Given a sequence of words, print all anagrams together | Set 2
- Given a sequence of words, print all anagrams together using STL
- k-th missing element in increasing sequence which is not present in a given sequence
- Find a valid parenthesis sequence of length K from a given valid parenthesis sequence
- Print all possible combinations of r elements in a given array of size n
- Given a string, print all possible palindromic partitions
- Print all the possible arithmetic expressions for a given number
- Print all possible joints of a Trie constructed from a given list of strings
- Print all possible rotations of a given Array
- Print all possible palindromic string formed using any pair of given strings
- Minimum LCM and GCD possible among all possible sub-arrays
- Print all n-digit strictly increasing numbers
- Print all 3 digit repeating numbers in a very large number
- Print all K digit repeating numbers in a very large number
- Minimum operations required to transform a sequence of numbers to a sequence where a[i]=a[i+2]
- Convert an unbalanced bracket sequence to a balanced sequence
- Find original sequence from Array containing the sequence merged many times in order
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.
Improved By : Rajput-Ji