Python | Print all string combination from given numbers
Given an integer N as input, the task is to print the all the string combination from it in lexicographical order.
Examples:
Input : 191 Output : aia sa Explanation: The Possible String digit are 1, 9 and 1 --> aia 19 and 1 --> sa Input : 1119 Output : aaai aas aki kai ks
Approach:
- Get the String and find all its combination list in the given order.
- Find the list whose integers lies in the range of 0 to 25.
- Convert the integers to the alphabets.
- Sort it in lexicographical order.
# Python program to print all string # combination from given numbers # Function to find the combination def Number_to_String(String): # length of string length = len(String) # temporary lists temp1 =[] temp2 =[] # alphabets Sequence alphabet ="abcdefghijklmnopqrstuvwxyz" # Power variable power = 2**(length-1) for i in range(0, power): # temporary String sub = "" Shift = i x = 0 sub += String[x] x += 1 for j in range(length - 1): if Shift&1: sub+=" " Shift = Shift>>1 sub += String[x] x += 1 temp1.append(list(map(int, sub.split()))) # Integer to String for index in temp1: substring ="" for j in index: if j > 0 and j <= 26: substring += alphabet[j-1] if len(substring) == len(index): temp2.append(substring) # lexicographical order sorting print(*sorted(temp2), sep =" ") # Driver Code Number_to_String("191") Number_to_String("1991") Number_to_String("1532") Number_to_String("1191") |
chevron_right
filter_none
Output:
aia sa aiia sia aecb ocb aaia asa kia
Recommended Posts:
- Permutation and Combination in Python
- Python program to print all even numbers in a range
- Python program to print even numbers in a list
- Python program to print odd numbers in a List
- Python program to print all odd numbers in a range
- Python Program to Print Numbers in an Interval
- Python program to print negative numbers in a list
- Python program to print all Prime numbers in an Interval
- Python program to print positive numbers in a list
- Python program to print all negative numbers in a range
- Python program to print all Strong numbers in given list
- Python program to print all positive numbers in a range
- Python | Extract Combination Mapping in two lists
- Python Program to print all permutations of a given string
- Python program to print the substrings that are prefix of the given string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.



