Python | Print all string combination from given numbers
Last Updated :
16 Jan, 2019
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.
def Number_to_String(String):
length = len (String)
temp1 = []
temp2 = []
alphabet = "abcdefghijklmnopqrstuvwxyz"
power = 2 * * (length - 1 )
for i in range ( 0 , power):
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())))
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)
print ( * sorted (temp2), sep = " " )
Number_to_String( "191" )
Number_to_String( "1991" )
Number_to_String( "1532" )
Number_to_String( "1191" )
|
Output:
aia sa
aiia sia
aecb ocb
aaia asa kia
Share your thoughts in the comments
Please Login to comment...