Python Program to Find All the Permutations of a String Using Naive Method
# Compute all the Permutation of the String Using a Naive Method in Python
# Initialising string
ini_str = "Hello"
# Printing initial string
print("Initial string:", ini_str)
# Finding all permutation
result = []
def permute(data, i, length):
if i == length:
result.append(''.join(data) )
else:
for j in range(i, length):
# swap
data[i], data[j] = data[j], data[i]
permute(data, i + 1, length)
data[i], data[j] = data[j], data[i]
permute(list(ini_str), 0, len(ini_str))
# Printing result
print("Resultant permutations:", str(result))
Output:
Initial string: Hello
Resultant permutations: ['Hello', 'Helol', 'Hello', 'Helol', 'Heoll', 'Heoll', 'Hlelo', 'Hleol', 'Hlleo', 'Hlloe', 'Hlole', 'Hloel', 'Hlleo', 'Hlloe', 'Hlelo', 'Hleol', 'Hloel', 'Hlole', 'Holle', 'Holel', 'Holle', 'Holel', 'Hoell', 'Hoell', 'eHllo', 'eHlol', 'eHllo', 'eHlol', 'eHoll', 'eHoll', 'elHlo', 'elHol', 'ellHo', 'elloH', 'elolH', 'eloHl', 'ellHo', 'elloH', 'elHlo', 'elHol', 'eloHl', 'elolH', 'eollH', 'eolHl', 'eollH', 'eolHl', 'eoHll', 'eoHll', 'leHlo', 'leHol', 'lelHo', 'leloH', 'leolH', 'leoHl', 'lHelo', 'lHeol', 'lHleo', 'lHloe', 'lHole', 'lHoel', 'llHeo', 'llHoe', 'lleHo', 'lleoH', 'lloeH', 'lloHe', 'loHle', 'loHel', 'lolHe', 'loleH', 'loelH', 'loeHl', 'lelHo', 'leloH', 'leHlo', 'leHol', 'leoHl', 'leolH', 'lleHo', 'lleoH', 'llHeo', 'llHoe', 'lloHe', 'lloeH', 'lHleo', 'lHloe', 'lHelo', 'lHeol', 'lHoel', 'lHole', 'lolHe', 'loleH', 'loHle', 'loHel', 'loeHl', 'loelH', 'oellH', 'oelHl', 'oellH', 'oelHl', 'oeHll', 'oeHll', 'olelH', 'oleHl', 'olleH', 'ollHe', 'olHle', 'olHel', 'olleH', 'ollHe', 'olelH', 'oleHl', 'olHel', 'olHle', 'oHlle', 'oHlel', 'oHlle', 'oHlel', 'oHell', 'oHell']
About The Author
I have more than 6 years of experience in the IT industry, and I have built a solid foundation in the creation and upkeep of online applications. I have constantly shown strong work ethics, self-motivation, and an aptitude for learning quickly throughout my career.