0

I tried to resolve this question https://leetcode.com/problems/unique-email-addresses/

The solution uses the index() method.

But it will raise ValueError when the substring is not found and will cause program interruption.

So I think the find() method may be better since it returns -1 on failure.

But I am not sure if the find() method has a better time complexity than the index() method.

Is there a way to find the time complexity of them?

  • 1
    Both have linear complexity as can be expected. Also what's the difference? In one case you catch the Exception, in the other you handle the -1 case... – user2390182 Dec 07 '20 at 13:38
  • 1
    [Iteration of a list is `O(n)`](https://wiki.python.org/moin/TimeComplexity), whether you `return` or `raise` doesn't affect the complexity – Tomerikoo Dec 07 '20 at 13:39
  • 1
    Depending on how `raise` is implemented, its time complexity might theoretically include a term for building the stack trace, which should be linear in the stack depth. But nobody really considers such things when analysing algorithms. – kaya3 Dec 07 '20 at 13:57
  • Would it be better to use find() instead of index() though both works for the question? –  Dec 08 '20 at 02:19

0 Answers0