![]() ![]() If not any(stset.issubset(s) for s in superstrings):ġ00000 loops, best of 3: 18. (frozenset(s.split()) for s in strings), key=len, reverse=True) It turns out this is faster than pre-sorting the strings: def using_sorted(strings): This is why we converted strings into a list of But frozensets are, so it is possible to have a set ofįrozensets. The items in a set have to be hashable, and sets themselves are not And finally, we would like superstrings to be a set of sets.So, instead, iterate over a copy: for sup in py(): In this case, remove the item in superstrings and replace it with s.īecause we are removing items from superstrings, we can not also S a superset of some item in superstrings. Superstrings if they are not a subset of a item already inįor each s, iterate through a set of superstrings: for sup in superstrings.Ĭheck if s <= sup - that is, if s is a subset of sup, quit the loop since s is smaller than some known superstring. As we examine each s in strings, we will add new ones to.Iterate through strings: for s in strings.Start with an empty set of superstrings = set().This is exactly what we need to determine if one string is a superstring of another. ![]() The reason why we want sets at all is because that have a convenient superset comparison operator: In : frozenset() <= frozenset() The reason for frozensets will become apparent shortly. We are given a list of strings, but what we'd really like - what would be more useful - is a list of sets: In : strings =
0 Comments
Leave a Reply. |