string = "abcd" I'm looking for the formula which gives all the possible substrings but the substrings are limited in length.
For example all possible substrings for "abcd" are 4*(4+1)/2 = 10 But here I'm looking for all the possible substrings which have a max length of 2, the result is 7 but what is the formula ?
thanks a lot for your help
EDIT: I'm interested of the number of substring possible, in order, with a max length of 2, for example "abcd" => a, b, c, d, ab, bc, cd => 7 "1111" => 1, 1, 1, 1, 11, 11, 11 => 7
A comment from @lulu that makes my question more clear: I believe the OP is requiring the substring to consist of consecutive elements of the string. Furthermore, judging from the answer to my question about 1111, the OP isn't interested in the string itself...just in its start and end location
To sum up the discussion in the comments: the reference to substrings is misleading as the OP is not interested in the characters that make up the substring, only in the possible locations of their first and last elements. Thus, the question is asking "given two natural numbers $m≤n$, how many pairs $(i,j)$ are possible with $1≤i<j≤n$ and $j-i≤m$?"
Let $F(n,m)$ denote the desired answer.
Example: for $m=2$, there are $n$ places that might be the first element of a length $1$ substring, and there are $n-1$ that might be the first element of a continuous length $2$ substring. Thus $$F(n,2)=n+(n-1)=2n-1$$
In general, there are $n-(k-1)$ places that might be the start of a continuous length $k$ substring. Thus the answer is $$F(n,m)=n+(n-1)+\cdots + (n-(m-1))=m\times n -\sum_{i=0}^{m-1} i=m\times n-T_{m-1}$$
Where $T_i$ denotes the $i^{th}$ Triangular Number. Thus $$T_i=\frac {i(i+1)}2$$
Examples:
$$F(n,2)=2n-T_1=2n-1$$ $$F(4,4)=4\times 4-T_3=16-6=10$$