6

and thanks for help

words = [....#Big list of words]
words_set = set(words)

I have hard time determine what is the complexity of set(words) when n=len(words). Is it O(n) since it moves on all the items of the list, or O(l(n-l)) when l is a single word length? Thanks for help! If there is a difference between WC and BC too.

Edit: don't mind O(l(n-l)) it's mistake for repeating substring big O.

nadir
  • 175
  • 1
  • 2
  • 8
  • I don't think "word length" is of significant importance, or rather assuming all words are the same length it should be a constant influence and thus insignificant. – PeterE Jan 07 '15 at 17:38

1 Answers1

6

I don't understand your second option, but iterating a list is O(n) and you must iterate the list to convert it to a set. Any operation on each element - eg hashing - is a constant factor which is dominated by the linear time of iteration.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • 1
    You are right, I confused it with another algorithm complexity. So it's O(n). Thanks very much – nadir Jan 07 '15 at 18:29
  • @nadirc: note: an *individual* `a_set.add(word)` might be `O(n)` but it is amortized `O(1)` unless [`words` list is constructed specifically to cause quadratic behavior](http://bugs.python.org/issue13703#msg150522). – jfs Jan 08 '15 at 10:56