How many subtrings are there in a string in general?
Why does string x [1:n] have O(n^2) subtrings according to the lecture 21 Dynamic Programming III of
6.006 from MIT?
Why it is not O(2^n)?
Here is a link [http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/lecture-videos/MIT6_006F11_lec21.pdf]
Approach: The count of sub-strings of length n will always be len – n + 1 where len is the length of the given string.
The total number of substrings formed by string of length N is (N*(N+1))/2, initialise count as (N*(N+1))/2.
Python String count()The count() method returns the number of occurrences of a substring in the given string.
Simply a substring is defined by two parameters [i,j]
which are start index and end index for substring in the original string . Now 0<=i,j<=n
as indices should be within the string, Total values i&j
each can have are n so all combinations of [i,j]
would be n*(n+1)/2 which is O(n^2)
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