How many string substrings

How many substrings are there in a string?

Why does string x [1:n] have O(n^2) subtrings in the lecture 21 Dynamic Programming III of        
6.006 from MIT? 
Why is not O(2^n)?

Here is the link [ http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/lecture-videos/MIT6_006F11_lec21.pdf]

+4
source share
5 answers

Just a substring is determined by two parameters [i,j], which are the starting index of the index and the end for the substring in the source string. Now 0<=i,j<=n, since codes must be in line, Total values i&jeach may have n, therefore, all combinations [i,j]are n*nthat areO(n^2)

+12

n = 4, : "ABCD"

( ):

  • 1 : A, B, C, D (4)
  • 2 : AB, BC, CD, ( 3 )
  • 3 : ABC, BCD ( 2 )
  • 4 : ABCD ( 1 )

: 1 + 2 + 3 + 4 = 10.

, , - 1 n.

(n ^ 2 + n)/2 (. )

, n ^ 2.

+17

, , , , , , , , , 2 ^ n, .

+1

n ,

1- , n

2- , n-1 .... ...

, 1234

1,12,123,1234

2,23,234

3,34

4

, n + (n-1) + (n-2) ...1 i.e. n , n(n+1)/2

0

, . .

string.Substring(0, 10); // untill 10th character, and so on

http://msdn.microsoft.com/en-us/library/aka44szs(v=vs.110).aspx

If you try to get a line using foreach, you will get each line separated by a space character. Thus, the space character will indicate the number of lines inside the line.

string strngs = "Love for all, Hatred for none!";

foreach (string strng in strngs) {
   Console.WriteLine(strng);
}

// (Expected) Output
// Love
// for
// all,
// Hatred
// for
// none!
-2
source

All Articles