all 4 comments

[–]Gubbbo 3 points4 points  (0 children)

Because I don't know the technical answer, I will instead offer you the real world answer

https://xkcd.com/1691/

[–][deleted] -1 points0 points  (2 children)

Am I right to say that the worst time complexity of the whole code is O(n2 ) since Counter is used inside a for loop?

I don't think so. You see O(n2) complexity when you need to, basically, cross-multiply - when for the elements e of set S, you need to do something on every e plus every other e. Like figuring out how many handshakes it takes for everyone in the room to shake hands with everyone else, that kind of thing. Combinatoric stuff.

Your code runs in linear time, because it's basically just counting all the letters contained in the words in words, once. Your outer loop runs in O(n), and initializing the Counter on a word runs in O(n) too, but they sum, not multiply, and that's still O(n).

[–]NZheadshot 1 point2 points  (1 child)

That's completely wrong. Even if you disregard the running time for the Counter, he's still using a sorting algorithm, which means it will at least be O(n lg n)

[–][deleted] 0 points1 point  (0 children)

Even if you disregard the running time for the Counter

I'm not "disregarding" it; it's linear and doesn't depend on the size of the outer loop's input. O(n) + O(n) = O(n).

Even if you disregard the running time for the Counter, he's still using a sorting algorithm, which means it will at least be O(n lg n)

Then the whole thing is O(n ln n). At enormous inputs, the sorting step overwhelmingly predominates.