Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the complexity of this function?

I'm practising in Codeacademy and I have to make the following function:

Define a function called anti_vowel that takes one string, text, as input and returns the text with all of the vowels removed

This is my solution.

def anti_vowel(text):
md = ""
for ch in text:
    if ch not in "aeiouAEIOU":
        md =  md + ch
return md 

It works well but I'm wondering what the complexity of the function is.

I think it's O(nk) where n:="length of text" and k:="length of "aeoiuAEIOU"".
I take one element of text and compare it with all vowels, that takes O(k) time. But I repeat that n times, so I do it all in O(nk). Is my analysis correct? How could I improve my function? Could it be linear?

like image 566
GniruT Avatar asked Dec 18 '22 20:12

GniruT


1 Answers

Big-O complexity doesn't work like that. k (the length of the vowels) is a constant, it doesn't change depending on the length of the input. So we discount it in calculating the complexity.

Your function is just O(n), ie linear complexity.

like image 109
Daniel Roseman Avatar answered Dec 21 '22 11:12

Daniel Roseman