3 ms·
Here's a shorter and much faster solution: [w for w in words if set(w) <= set(puzzle) and puzzle[4] in w] Edit: Oh I see you can only use each letter once
by jules 16y ago
Here's a shorter and much faster solution:
[w for w in words if set(w) <= set(puzzle) and puzzle[4] in w]
Edit: Oh I see you can only use each letter once. In that case you'd use a multiset here instead of set, which Python doesn't support natively unfortunately.
from collections import defaultdict
class bag:
def __init__(self, xs):
self.dict = defaultdict(int)
for x in xs: self.dict[x] += 1
def __le__(self, other):
return all(self.dict[x] <= other.dict[x] for x in self.dict.keys())
And the solution becomes:
[w for w in words if bag(w) <= bag(puzzle) and puzzle[4] in w]