python check string contains all characters

后端 未结 4 1625
粉色の甜心
粉色の甜心 2021-01-29 11:10

I\'m reading in a long list of words, and I made a node for every word in the list. Each node has an attribute \'word\' for their position in the list.

I am trying to co

4条回答
  •  情歌与酒
    2021-01-29 11:41

    The problem is that you are only comparing words that appear right next to each other in the list, i.e. words i and i+1, e.g. I and IN appear next to each other, as do WIN and WIND, but IN and WIND are far apart. It seems you want to compare all possible words, which requires a more sophisticated algorithm. Here's an idea:

    1. Make a dictionary where they keys are sorted words and the values are lists of actual words, e.g. {"ACT": ["CAT", "ACT", "TAC], ...}. A collections.defaultdict(list) will be useful for this.
    2. Sort the full input list of words by length. You can use list.sort(key=len) assuming you have just a list of words.
    3. Iterate through the list sorted by length. For each word, go through every subset of length n-1. Something like for i in range(len(word)): process(word[:i] + word[i+1:]). You may want to be careful about duplicates here.
    4. For each subset, sort the subset and look it up in the dictionary. Make a link from every word in the dictionary's value (a list of actual words) to the bigger word.

提交回复
热议问题