If I have a list of words in the variable words and a list of letters in the variable letters, how can I find all the words that can be made up out of the letters in letters. Any subset of the letters in the variable letters can be used and letters can be used more than once. I would like to do this in Python.
For example:
letters = ['a', 'b', 'i', 'l', 'r', 's', 't', 'u']
words = ['dummy', 'australia']
Should return:
'australia'
even though there is an additional 'b', but not:
'dummy'
since d, m and y are not available.
Use regular expressions:
This employs set intersection, which might be faster. On the other hand, it requires additional memory.
s is now
['australia']
(But I'm curious about the use of such a solution. A Scrabble robot? A dusty-keyboard attack on dictionary passwords? )
you can use
all()
along withsets
, because they allowO(1)
membership checkup:This is probably the easiest way:
This returns
['australia']