What is the complexity of this function?

I train at Codeacademy and I need to do the following function:

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

This is my decision.

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

This works well, but I wonder what the complexity of the function is.

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

+4
source share
2

, O(nk). , , , O(nk) O(n), k - , . , .

text n. , k (k - !). k.

, :

O(k)O(n) = O(kn) = O(n)

, , , .

+4

Big-O . k ( ) , . .

- O (n), .

+7

Source: https://habr.com/ru/post/1622469/


All Articles