I assume you provide A
and B
just for example of their contents, and you are aware that complexity has meaning only for sets of inputs (like average, worst and best cases), not for individual inputs.
My point is, depending on the problem requirements and use case, you can make very different estimates for the code complexity.
Let n
be A.Length
and m
be B.Length
. Complexity of the given code then can be calculated in a few different ways:
Assume string.Contains
is O(1)
. In practice, such strong assumption can be made, for example, if we know for sure that no string is longer than some fixed in advance length. Then code complexity is of course O(n*m)
.
Assume string.Contains
is O(x*y)
, where x
and y
are lengths of haystack and needle. Let the longest string in A
be of length k_A
and the longest string in B
be of length k_B
. Then code complexity would be O(n*m*k_A*k_B)
For the second case, there is another (more practical) approach:
Let S_A
be the sum of lengths for all strings in A
, and S_B
be the sum of length for all strings in B
. Then code complexity would be O(S_A * S_B)
.
That's it for the worst case. For the average case, though, and for the most practical cases, string.Contains
is O(x + y)
. So average code complexity would be O(n*m*(k_A + k_B))
or O((S_B + k_A) * n + (S_A + k_B) * m)
.