Given an integer, S represented as a string, get the sum of all possible substrings of this string.
belongs to collection: Interview C++ coding problems/challenges | String
All Answers
total answers (1)
belongs to collection: Interview C++ coding problems/challenges | String
total answers (1)
The solution approach is by storing the substring sums to compute the exact next substring sum
All the statements are self-explanatory except the one which is the fundamental idea of the entire storing process. That is the below one,
Let's check this with an example,
Let's start for the same example
N=4 here
So, we need to fill up a 4X4 DP table,
After filling the base case,
Now, I am computing for len=2
Start=0, end=1
Start=1, end=2
Start=2, end=3
For len =3
Start=0, end=2
Start=1, end=3
Len=4
Start=0, end=3
At each step we have summed up, so result is stored at sum.
C++ Implementation:
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer