I'm trying to rate the efficiency of a function where the input is an array of strings. The algorithm always iterates through every item in this array. This strings contained in this array are of variable length. In this initial for loop, a character replace function is called on each string. I believe the replace function on its own would be O(n) where n is the length of the string.
So I'm confused how to evaluate big o efficiency here. If n is the size of the array, I know it will at least be O(n). But with variable string lengths, how would you rate the overall efficiency with the string replacement? Would you say n is the size of the array and use other variables to represent the different sizes of each string?
Personally, I would express efficiency through size of input data (as opposed to the length of array). So, if input is t
bytes, running time will be O(t)
. And t
here is also a common length of all strings.
I see two ways to say this (out of many possible).
The first is to say it is O(N)
where N
is the total number of characters.
The other you could say is O(N*M)
where N
is the number of strings and M
is the average number of characters per string. Note that this is actually the same as my above answer. You could say M = k/N
, so you get O(N*k/N)
or O(k)
where k is the total characters in all the strings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With