I'm writing a program that can take a string input and find all the possible dictionary words that can be made from the letters in the input. My current solution is:
for i in range(len(userInput) + 1):
permutations = list(itertools.permutations(userInput, i))
for letter in permutations:
joinedPermutations = ''.join(letter)
if joinedPermutations not in wordCombinations and len(joinedPermutations) > 1: # wordCombinations stores all the matching words.
for lines in fetchedFiles: # fetchedFiles is a list of all the dictionary words of a given letter.
if joinedPermutations in lines:
wordCombinations.append(joinedPermutations)
This method works well for small inputs however, once larger inputs are used, it can take hundreds of thousands of passes to find all the permutations. I do understand using permutations isn't the most efficient method of finding combinations, but I lack the knowledge to make it more efficient.
What would be a more efficient method of finding dictionary words from an input string?
I don't necessarily need to use this method I've created, it is just the only one I thought of.