1

I got a really long time complexity for this function, not sure if we can simplify it.

# Let n = # of file names in the input
# Overall Time Complexity: O(n + k_0 + k_1 + k_2 + ... + k_n + 1) = O(n + k_0 + k_1 + k_2 + ... + k_n)
def split_input(input):

    # Time Complexity: O(n)
    file_names = input.split(",")

    # Let k_i = file name length for the i^th element
    # Time Complexity: O(k_0 + k_1 + k_2 + ... + k_n)
    for index, value in enumerate(file_names):
        # O(k_i)
        file_names[index] = file_names[index].strip()

    # Time Complexity: O(1)
    return file_names

1 Answer 1

1

You have already defined n here as the total length of your input string, which includes all your file names, so

n > k_0 + k_1 + ... + k_n.

Therefore, your time complexity is just O(n).

Sign up to request clarification or add additional context in comments.

3 Comments

So we define "n" as the # of characters in the string rather than the # of actual file names in the string? e.g. "abc.csv, def.csv, ghi.csv"
@NoName Well you already claim that input.split(",") is O(n) so clearly you have chosen the total length of the string as your n, right? str.split() does have to go through all characters of the string.
If you prefer to define n as your number of file names, then your time complexity would be O(k0 + k1 + ... + kn)) just O(n * k_avg) where k_avg is your average file name length. Can also express it as O(n * k_longest), though that might be a less tight bound.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.