Question

Suppose that I have a set of strings. If a string is a substring of another string, then the former should be removed the set.

My idea is to iterate all the strings in the original set, and for each string test against other strings in the set and remove any string that is a substring of others in the original set. But this results in in-place modification of the original set, which might cause some problems in the implementation.

Does anybody one have a better idea how this should be implemented? Thanks.

Was it helpful?

Solution

Your question is not very clear. But if I understand you correctly, you can do something like this

l = sorted(["abcd", "abc", "ab", "a"], key = len)
print [ss for idx, ss in enumerate(l) if all(ss not in cs for cs in l[idx + 1:])]

Output

['abcd']
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top