DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Discussion on: Solution: Word Subsets

Collapse
 
oliverandrich profile image
Oliver Andrich • Edited on

Thanks for sharing. Another nice site to find some nice problems to solve in my spare time. I like to share a bit of an obscure solution for Python with you.

class Solution:
    def wordSubsets(self, A: List[str], B: List[str]) -> List[str]:
        return [word for word in A if all([len(set(c) - set(word)) == 0 for c in B])]
Enter fullscreen mode Exit fullscreen mode
Collapse
 
seanpgallivan profile image
seanpgallivan Author

Unfortunately, that doesn't account for multiple occurrences of the same letter.

The instructions state:

Now, say that word b is a subset of word a if every letter in b occurs in a, including multiplicity. For example, "wrr" is a subset of "warrior", but is not a subset of "world".

The code you posted would find "wrr" as a subset of "world", because the set will only compare a single "r" instead of requiring 2 for a match.

It's still a remarkable piece of code, however! One of the things that impresses (and intimidates) me the most about Python is its ability to allow for truly concise code.