There is a string list, for example ["abc", "ab", "ad", "cde", "cde", "de", "def"] I would like the output to be ["abc", "ad", "cde", "def"]
"ab" was removed because it is the substring of "abc" "cde" was removed because it is the substring of another "cde" "de" was removed because it is the substring of "def"
What is the fastest algorithm?
I have a brute-force method, which is O(n^2) as follows:
def keep_long_str(str_list):
str_list.sort(key = lambda x: -len(x))
cleaned_str_list = []
for element in str_list:
element = element.lower()
keep_element = 1
for cleaned_element in cleaned_str_list:
if element in cleaned_element:
keep_element = 0
break
else:
keep_element = 1
if keep_element:
cleaned_str_list.append(element)
return cleaned_str_list
["cde", "de"], would"de"be removed?