# How to derive the equation for all substrings of a string

Hi there,
I am studying a course for Data Structures on freecodecamp Youtube channel.

I couldn’t figure out how to calculate the derived equation for all substrings n(n+1)/2 (as shown in the picture).

number of substrings in a string with n characters = n(n+1)/2

5 characters → 15 substrings
6 characters → 21 substrings
7 characters → 28 substrings

With n = 5 you have 15 = 5 * 3 → 5 * 6/2 → n * (n+1)/2
With n = 6 you have 21 = 6 * 3 + 3 → 3 * (6 + 1) → 6/2 * (6+1) → n/2*(n+1) → n * (n+1)/2
With n = 7 you have 28 = 7 * 4 → 7 * 8/2 → n * (n+1)/2

@willjw3 I need to drive it, my apology, I didn’t make it clear that I don’t want to prove it result to this, but I want to drive it … I want to make the steps that show at the end it result to n(n+1)/2

Or this is how It should be derived? hmm, not sure

It is just I am using another values of n, and trying to derive its equation like I don’t know if it is n(n+1)/2 or something else … but I cannot derive it.

That was my derivation of the result. Basically, I looked at some values and found a pattern relating to the number of characters, n.

It takes a bit of time to see the pattern. You have to look at the number of substrings for a few given values of n (which you can count easily for small values of n) and see if you can find a relation between n and the number of substrings.

@willjw3
What kind of math we learn years ago so we can think about derivation like this? I may think of study some to revise some rules.

I don’t remember exactly, but I used to teach Algebra at the high school level, and my students did exercises like these.

1 Like

I think I could derive it now, based on using multiplication way. Thank you @willjw3

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.