# Sum of all substrings of a string representing a number | Set 1

Given a integer represented as a string, we need to get the sum of all possible substrings of this string.

Examples:

```Input  : num = “1234”
Output : 1670
Sum = 1 + 2 + 3 + 4 + 12 + 23 +
34 + 123 + 234 + 1234
= 1670

Input  : num = “421”
Output : 491
Sum = 4 + 2 + 1 + 42 + 21 + 421 = 491
```

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

We can solve this problem using dynamic programming. We can write summation of all substrings on basis of digit at which they are ending in that case,
Sum of all substrings = sumofdigit + sumofdigit + sumofdigit … + sumofdigit[n-1] where n is length of string.

Where sumofdigit[i] stores sum of all substring ending at ith index digit, in above example,

```Example : num = "1234"
sumofdigit = 1 = 1
sumofdigit = 2 + 12  = 14
sumofdigit = 3 + 23  + 123 = 149
sumofdigit = 4 + 34  + 234 + 1234  = 1506
Result = 1670
```

Now we can get the relation between sumofdigit values and can solve the question iteratively. Each sumofdigit can be represented in terms of previous value as shown below,

```For above example,
sumofdigit = 4 + 34 + 234 + 1234
= 4 + 30 + 4 + 230 + 4 + 1230 + 4
= 4*4 + 10*(3 + 23 +123)
= 4*4 + 10*(sumofdigit)
In general,
sumofdigit[i]  =  (i+1)*num[i] + 10*sumofdigit[i-1]
```

Using above relation we can solve the problem in linear time. In below code a complete array is taken to store sumofdigit, as each sumofdigit value requires just previous value, we can solve this problem without allocating complete array also.

## C++

 `// C++ program to print sum of all substring of ` `// a number represented as a string ` `#include ` `using` `namespace` `std; ` ` `  `// Utility method to convert character digit to ` `// integer digit ` `int` `toDigit(``char` `ch) ` `{ ` `    ``return` `(ch - ``'0'``); ` `} ` ` `  `// Returns sum of all substring of num ` `int` `sumOfSubstrings(string num) ` `{ ` `    ``int` `n = num.length(); ` ` `  `    ``// allocate memory equal to length of string ` `    ``int` `sumofdigit[n]; ` ` `  `    ``// initialize first value with first digit ` `    ``sumofdigit = toDigit(num); ` `    ``int` `res = sumofdigit; ` ` `  `    ``// loop over all digits of string ` `    ``for` `(``int` `i = 1; i < n; i++) { ` `        ``int` `numi = toDigit(num[i]); ` ` `  `        ``// update each sumofdigit from previous value ` `        ``sumofdigit[i] = (i + 1) * numi + 10 * sumofdigit[i - 1]; ` ` `  `        ``// add current value to the result ` `        ``res += sumofdigit[i]; ` `    ``} ` ` `  `    ``return` `res; ` `} ` ` `  `// Driver code to test above methods ` `int` `main() ` `{ ` `    ``string num = ``"1234"``; ` `    ``cout << sumOfSubstrings(num) << endl; ` `    ``return` `0; ` `} `

## Java

 `// Java program to print sum of all substring of ` `// a number represented as a string ` `import` `java.util.Arrays; ` ` `  `class` `GFG { ` ` `  `    ``// Returns sum of all substring of num ` `    ``public` `static` `int` `sumOfSubstrings(String num) ` `    ``{ ` `        ``int` `n = num.length(); ` ` `  `        ``// allocate memory equal to length of string ` `        ``int` `sumofdigit[] = ``new` `int``[n]; ` ` `  `        ``// initialize first value with first digit ` `        ``sumofdigit[``0``] = num.charAt(``0``) - ``'0'``; ` `        ``int` `res = sumofdigit[``0``]; ` ` `  `        ``// loop over all digits of string ` `        ``for` `(``int` `i = ``1``; i < n; i++) { ` `            ``int` `numi = num.charAt(i) - ``'0'``; ` ` `  `            ``// update each sumofdigit from previous value ` `            ``sumofdigit[i] = (i + ``1``) * numi + ``10` `* sumofdigit[i - ``1``]; ` ` `  `            ``// add current value to the result ` `            ``res += sumofdigit[i]; ` `        ``} ` ` `  `        ``return` `res; ` `    ``} ` ` `  `    ``// Driver code to test above methods ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``String num = ``"1234"``; ` ` `  `        ``System.out.println(sumOfSubstrings(num)); ` `    ``} ` `} ` `// This code is contributed by Arnav Kr. Mandal. `

## Python3

 `# Python program to print  ` `# sum of all substring of ` `# a number represented as  ` `# a string ` ` `  `# Returns sum of all  ` `# substring of num ` `def` `sumOfSubstrings(num): ` `    ``n ``=` `len``(num) ` ` `  `    ``# allocate memory equal  ` `    ``# to length of string ` `    ``sumofdigit ``=` `[] ` ` `  `    ``# initialize first value ` `    ``# with first digit ` `    ``sumofdigit.append(``int``(num[``0``])) ` `    ``res ``=` `sumofdigit[``0``] ` ` `  `    ``# loop over all ` `    ``# digits of string ` `    ``for` `i ``in` `range``(``1``, n): ` `        ``numi ``=` `int``(num[i]) ` ` `  `        ``# update each sumofdigit ` `        ``# from previous value ` `        ``sumofdigit.append((i ``+` `1``) ``*`  `                        ``numi ``+` `10` `*` `sumofdigit[i ``-` `1``]) ` ` `  `        ``# add current value ` `        ``# to the result ` `        ``res ``+``=` `sumofdigit[i] ` ` `  `    ``return` `res ` ` `  `# Driver code  ` `num ``=` `"1234"` `print``(sumOfSubstrings(num)) ` ` `  `# This code is contributed  ` `# by Sanjit_Prasad `

## C#

 `// C# program to print sum of ` `// all substring of a number ` `// represented as a string ` `using` `System; ` ` `  `class` `GFG { ` ` `  `    ``// Returns sum of all ` `    ``// substring of num ` `    ``public` `static` `int` `sumOfSubstrings(String num) ` `    ``{ ` `        ``int` `n = num.Length; ` ` `  `        ``// allocate memory equal to ` `        ``// length of string ` `        ``int``[] sumofdigit = ``new` `int``[n]; ` ` `  `        ``// initialize first value ` `        ``// with first digit ` `        ``sumofdigit = num - ``'0'``; ` `        ``int` `res = sumofdigit; ` ` `  `        ``// loop over all digits ` `        ``// of string ` `        ``for` `(``int` `i = 1; i < n; i++) { ` `            ``int` `numi = num[i] - ``'0'``; ` ` `  `            ``// update each sumofdigit ` `            ``// from previous value ` `            ``sumofdigit[i] = (i + 1) * numi + 10 * sumofdigit[i - 1]; ` ` `  `            ``// add current value ` `            ``// to the result ` `            ``res += sumofdigit[i]; ` `        ``} ` ` `  `        ``return` `res; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``String num = ``"1234"``; ` ` `  `        ``Console.Write(sumOfSubstrings(num)); ` `    ``} ` `} ` ` `  `// This code is contributed by Nitin Mittal. `

## PHP

 ` `

Output:

```1670
```

Time Complexity : O(n) where n is length of input string.
Auxiliary Space : O(n)

Sum of all substrings of a string representing a number | Set 2 (Constant Extra Space)

This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

O(1) space approach
Approach is same as described above. What we have observed that at current index we are dependent on current sum + previous index sum so instead of storing in a dp array we can store it in two variables current and prev.

 `// C++ program to print sum of all substring of ` `// a number represented as a string ` `#include ` `using` `namespace` `std; ` ` `  `// Utility method to convert character digit to ` `// integer digit ` `int` `toDigit(``char` `ch) ` `{ ` `    ``return` `(ch - ``'0'``); ` `} ` ` `  `// Returns sum of all substring of num ` `int` `sumOfSubstrings(string num) ` `{ ` `    ``int` `n = num.length(); ` ` `  `    ``// storing prev value ` `    ``int` `prev = toDigit(num); ` ` `  `    ``int` `res = prev; ``// ans ` ` `  `    ``int` `current = 0;  ` ` `  `    ``// substrings sum upto current index ` `    ``// loop over all digits of string ` `    ``for` `(``int` `i = 1; i < n; i++) { ` `        ``int` `numi = toDigit(num[i]); ` ` `  `        ``// update each sumofdigit from previous value ` `        ``current = (i + 1) * numi + 10 * prev; ` ` `  `        ``// add current value to the result ` `        ``res += current; ` `        ``prev = current; ``// update previous ` `    ``} ` ` `  `    ``return` `res; ` `} ` ` `  `// Driver code to test above methods ` `int` `main() ` `{ ` `    ``string num = ``"1234"``; ` `    ``cout << sumOfSubstrings(num) << endl; ` `    ``return` `0; ` `} `

Output:

```1670
```