fork(1) download
  1. import collections
  2. def filterSublist1(words):
  3. longest = collections.defaultdict(str)
  4. for word in words: # O(n)
  5. for i in range(1, len(word)+1): # O(k)
  6. for j in range(len(word) - i + 1): # O(k)
  7. subword = word[j:j+i] # O(1)
  8. if len(longest[subword]) < len(word): # O(1)
  9. longest[subword] = word # O(1)
  10.  
  11. return list(set(longest.values())) # O(n)
  12. # Total: O(nk²)
  13.  
  14. print filterSublist1(['a', 'abc', 'b', 'd', 'xy', 'xyz'])
  15. print filterSublist1(['ab', 'bc', 'ac'])
  16. print filterSublist1(['abb', 'ba'])
Success #stdin #stdout 0.01s 7864KB
stdin
Standard input is empty
stdout
['xyz', 'abc', 'd']
['ac', 'ab', 'bc']
['abb', 'ba']