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?
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.
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