Sum of all substrings of a number
Given an integer, S represented as a string, get the sum of all possible substrings of this string.
A string S that representing the number.
Print sum of all possible substrings as required result.
1 <= T <= 100 1 <= S <= 1012
Input: 1234 326 Output: 1670 395
For the first input 1234, All possible substrings are 1, 2, 3, 4, 12, 13, 23, 34, 123, 234, 1234 Total sum = 1 + 2 + 3 + 4 + 12 + 23 + 34 + 123 + 234 + 1234 = 1670 For the second input 326 All possible substrings are 3, 2, 6 32, 26 326 Total sum=3+2+6+32+26+326= 395
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
So, we need to fill up a 4X4 DP table,
After filling the base case,
Now, I am computing for len=2
For len =3
At each step we have summed up, so result is stored at sum.
Output:need an explanation for this answer? contact us directly to get an explanation for this answer