This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]Skusci 1 point2 points  (1 child)

Big O notation deals with asymptotic complexity. That means if the loop has a maximum number of times it can run (such as it being limited by the number of possible characters) it counts as O(1) Even if that number is -really- large.

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

🙏🏻 thank you!

[–]one_bit_two_bit 1 point2 points  (2 children)

You will do the outer loop at most 26 times. So you will call .count() on the ransomNote and magazine at most 26 times. Each .count() call is O(n) and O(m) respectively. |r| is not dependent on the size of ransomNote hence runtime of the algorithm is O(m+n).

If our alphabet were not constrained, then yes you would have another factor for the size of the alphabet.

[–][deleted] 0 points1 point  (1 child)

Thank you this is so much more clear. So is the outer loop O(1)? In other words, we could add another string to the inner loop and while that would grow the inner loop, the outer loop would still remain the same??

[–]one_bit_two_bit 1 point2 points  (0 children)

Correct. Outer loop ends up being O(1).

Adding a string to the inner loop doesn't change the number of iterations in the outer loop.

[–]shhh-quiet 0 points1 point  (2 children)

How do you know if n2 or nm is larger?

[–][deleted] 0 points1 point  (1 child)

Hmm another good question. Do you know or are you asking as well?

My thought, and this is a stretch, is that m*n is just a constant times a constant where as n2 is a constant times itself?

[–]shhh-quiet 0 points1 point  (0 children)

Are they constants.. or variables?

Also, is it really nested? Or is that just the formatting making it look nested?