Trying to figure out the run time of my function

donut juice

I have this python code for finding the longest substring. I'm trying to figure out the asymptotic run time of it and I've arrived at an answer but I'm not sure if it's correct. Here is the code:

def longest_substring(s, t):
    best = ' '
    for s_start in range(0, len(s)):
        for s_end in range(s_start, len(s)+1):
            for t_start in range(0, len(t)):
                for t_end in range(t_start, len(t)+1):
                    if s[s_start:s_end] == t[t_start:t_end]:
                        current = s[s_start:s_end]
                            if len(current) > len(best):
                                best = current
    return best

Obviously this function has a very slow run time. It was designed that way. My approach was that because there is a for loop with 3 more nested for-loops, the run-time is something like O(n^4). I am not sure if this is correct due to not every loop iterating over the input size. Also, it is to be assumed that s = t = n(input size). Any ideas?

univerio

If you're not convinced that it's O(n^5), try calculating how many loops you run through for string s alone (i.e. the outer two loops). When s_start == 0, the inner loop runs n + 1 times; when s_start == 1, the inner loop runs n times, and so on, until s_start = n - 1, for which the inner loop runs twice.

The sum

(n + 1) + (n) + (n - 1) + ... + 2

is an arithmetic series for which the formula is

((n + 1) + 2) * n / 2

which is O(n^2).

An additional n factor comes from s[s_start:s_end] == t[t_start:t_end], which is O(n).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Trying to figure out why Excel freezes the second time I run the code

From Dev

Trying to figure out what this hash function returns

From Dev

The empty function seems to clear some classes from my wrapper, but not all. Trying to figure out whats wrong

From Dev

Trying to figure out pow source code in python | My function gone wrong

From Dev

How can my app figure out (at run-time) if it is running on Android Wear?

From Dev

Unable to figure out how to create and run my user-defined R function in Shiny with input text boxes

From Dev

Trying to figure out the logic of this current time and date code

From Dev

Trying to figure out kinematics

From Dev

trying to figure out how to add GestureDetectorCompat to my project

From Dev

trying to figure out how to add GestureDetectorCompat to my project

From Dev

Can't figure out how to use arguments for my function

From Dev

Verilog Function - Can't figure out my error

From Dev

Trying to figure out MASM syntax

From Dev

Trying To Figure Out CSS Animations

From Dev

Trying to figure out SQL Statement

From Dev

Trying to figure out recursive basics

From Dev

Trying to figure out block data

From Dev

Trying to figure out CompletionHandler in Swift?

From Dev

trying to figure out a route in laravel

From Dev

Why will this code not work? I have spent a long time trying to figure this out

From Dev

Can't figure out what goes wrong with my PHP code when trying to input information into database

From Dev

Trying to calculate EMA using python and i cant figure out why my code is always producing the same result

From Dev

JavaFX - Thread hangs, and I can't figure out how to run my loop outside the UI thread

From Dev

Trying to figure out a $watch on data from a service

From Dev

Trying to figure out 'bundle install' in Rails

From Dev

Trying to figure out this code, object in javascript

From Dev

Trying to figure out a portable data saving approach

From Dev

trying to figure out how to fill array in php

From Dev

trying to figure out a good database design

Related Related

  1. 1

    Trying to figure out why Excel freezes the second time I run the code

  2. 2

    Trying to figure out what this hash function returns

  3. 3

    The empty function seems to clear some classes from my wrapper, but not all. Trying to figure out whats wrong

  4. 4

    Trying to figure out pow source code in python | My function gone wrong

  5. 5

    How can my app figure out (at run-time) if it is running on Android Wear?

  6. 6

    Unable to figure out how to create and run my user-defined R function in Shiny with input text boxes

  7. 7

    Trying to figure out the logic of this current time and date code

  8. 8

    Trying to figure out kinematics

  9. 9

    trying to figure out how to add GestureDetectorCompat to my project

  10. 10

    trying to figure out how to add GestureDetectorCompat to my project

  11. 11

    Can't figure out how to use arguments for my function

  12. 12

    Verilog Function - Can't figure out my error

  13. 13

    Trying to figure out MASM syntax

  14. 14

    Trying To Figure Out CSS Animations

  15. 15

    Trying to figure out SQL Statement

  16. 16

    Trying to figure out recursive basics

  17. 17

    Trying to figure out block data

  18. 18

    Trying to figure out CompletionHandler in Swift?

  19. 19

    trying to figure out a route in laravel

  20. 20

    Why will this code not work? I have spent a long time trying to figure this out

  21. 21

    Can't figure out what goes wrong with my PHP code when trying to input information into database

  22. 22

    Trying to calculate EMA using python and i cant figure out why my code is always producing the same result

  23. 23

    JavaFX - Thread hangs, and I can't figure out how to run my loop outside the UI thread

  24. 24

    Trying to figure out a $watch on data from a service

  25. 25

    Trying to figure out 'bundle install' in Rails

  26. 26

    Trying to figure out this code, object in javascript

  27. 27

    Trying to figure out a portable data saving approach

  28. 28

    trying to figure out how to fill array in php

  29. 29

    trying to figure out a good database design

HotTag

Archive