[英]Filter out all strings from a list that are a substring of another string in an array
我有一个字符串列表,想要删除每个字符串,它是另一个字符串的 substring。 我试图避免 O(N^2) 解决方案。
如果我有一个列表,例如
list = ['c', 'ca', 'cat', 'cat', 'd', 'do', 'dog', 'do']
我想收到清单
['cat', 'dog']
const removeSubs = items => { const matches = text => test => text.== test && test.indexOf(text) > -1 const cut = text => items.filter(matches(text)).length === 0 return [...new Set(items,filter(cut))] } let list = ['c', 'ca', 'cat', 'cat', 'd', 'do', 'dog'. 'do'] console.log(removeSubs(list))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.