Given an integer represented as a string, we need to get the sum of all possible substrings of this string
Examples:
Input: num = “1234”
Output: 1670
Explanation: Sum = 1 + 2 + 3 + 4 + 12 + 23 +34 + 123 + 234 + 1234
= 1670
Input: num = “421”
Output: 491
Explanation: Sum = 4 + 2 + 1 + 42 + 21 + 421 = 491
Sum of all substrings of a string representing a number using Dynamic-Programming:
To solve the problem follow the below idea:
We can solve this problem by using dynamic programming. We can write a summation of all substrings on basis of the digit at which they are ending in that case,
Sum of all substrings = sumofdigit[0] + sumofdigit[1] + sumofdigit[2] … + sumofdigit[n-1] where n is length of string.
Where sumofdigit[i] stores the sum of all substring ending at ith index digit, in the above example,
Example: num = “1234”
sumofdigit[0] = 1 = 1
sumofdigit[1] = 2 + 12 = 14
sumofdigit[2] = 3 + 23 + 123 = 149
sumofdigit[3] = 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[3] = 4 + 34 + 234 + 1234
= 4 + 30 + 4 + 230 + 4 + 1230 + 4
= 4*4 + 10*(3 + 23 +123)
= 4*4 + 10*(sumofdigit[2])
In general, sumofdigit[i] = (i+1)*num[i] + 10*sumofdigit[i-1]
Follow the given steps to solve the problem:
- Declare an array of size n to store the sum of previous digits, processed so far, and a variable result
- Traverse over the string and for every character
- Set sumOfDigit[i] = (i + 1) * toDigit(num[i]) + 10 * sumOfDigit[i-1]
- Add the value of sumOfDigit[i] to result
- Return result
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int toDigit( char ch) { return (ch - '0' ); }
int sumOfSubstrings(string num)
{
int n = num.length();
int sumofdigit[n];
sumofdigit[0] = toDigit(num[0]);
int res = sumofdigit[0];
for ( int i = 1; i < n; i++) {
int numi = toDigit(num[i]);
sumofdigit[i]
= (i + 1) * numi + 10 * sumofdigit[i - 1];
res += sumofdigit[i];
}
return res;
}
int main()
{
string num = "1234" ;
cout << sumOfSubstrings(num) << endl;
return 0;
}
|
Java
import java.util.Arrays;
class GFG {
public static int sumOfSubstrings(String num)
{
int n = num.length();
int sumofdigit[] = new int [n];
sumofdigit[ 0 ] = num.charAt( 0 ) - '0' ;
int res = sumofdigit[ 0 ];
for ( int i = 1 ; i < n; i++) {
int numi = num.charAt(i) - '0' ;
sumofdigit[i]
= (i + 1 ) * numi + 10 * sumofdigit[i - 1 ];
res += sumofdigit[i];
}
return res;
}
public static void main(String[] args)
{
String num = "1234" ;
System.out.println(sumOfSubstrings(num));
}
}
|
Python3
def sumOfSubstrings(num):
n = len (num)
sumofdigit = []
sumofdigit.append( int (num[ 0 ]))
res = sumofdigit[ 0 ]
for i in range ( 1 , n):
numi = int (num[i])
sumofdigit.append((i + 1 ) *
numi + 10 * sumofdigit[i - 1 ])
res + = sumofdigit[i]
return res
if __name__ = = '__main__' :
num = "1234"
print (sumOfSubstrings(num))
|
C#
using System;
class GFG {
public static int sumOfSubstrings(String num)
{
int n = num.Length;
int [] sumofdigit = new int [n];
sumofdigit[0] = num[0] - '0' ;
int res = sumofdigit[0];
for ( int i = 1; i < n; i++) {
int numi = num[i] - '0' ;
sumofdigit[i]
= (i + 1) * numi + 10 * sumofdigit[i - 1];
res += sumofdigit[i];
}
return res;
}
public static void Main()
{
String num = "1234" ;
Console.Write(sumOfSubstrings(num));
}
}
|
PHP
<?php
function toDigit( $ch )
{
return ( $ch - '0' );
}
function sumOfSubstrings( $num )
{
$n = strlen ( $num );
$sumofdigit [ $n ] = 0;
$sumofdigit [0] = toDigit( $num [0]);
$res = $sumofdigit [0];
for ( $i = 1; $i < $n ; $i ++)
{
$numi = toDigit( $num [ $i ]);
$sumofdigit [ $i ] = ( $i + 1) * $numi + 10 *
$sumofdigit [ $i - 1];
$res += $sumofdigit [ $i ];
}
return $res ;
}
$num = "1234" ;
echo sumOfSubstrings( $num ) ;
?>
|
Javascript
<script>
function sumOfSubstrings(num)
{
let n = num.length;
let sumofdigit = new Array(n);
sumofdigit[0] = num[0].charCodeAt() - '0' .charCodeAt();
let res = sumofdigit[0];
for (let i = 1; i < n; i++) {
let numi = num[i].charCodeAt() - '0' .charCodeAt();
sumofdigit[i] = (i + 1) * numi + 10 * sumofdigit[i - 1];
res += sumofdigit[i];
}
return res;
}
let num = "1234" ;
document.write(sumOfSubstrings(num));
</script>
|
Time Complexity: O(N) where N is the length of the input string.
Auxiliary Space: O(N)
Sum of all substrings of a string representing a number using constant Space:
To solve the problem follow the below idea:
The approach is the same as described above. What we have observed is that at the current index we are dependent on the current sum + previous index sum so instead of storing it in an array we can store it in two variables current and prev
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int toDigit( char ch) { return (ch - '0' ); }
int sumOfSubstrings(string num)
{
int n = num.length();
int prev = toDigit(num[0]);
int res = prev;
int current = 0;
for ( int i = 1; i < n; i++) {
int numi = toDigit(num[i]);
current = (i + 1) * numi + 10 * prev;
res += current;
prev = current;
}
return res;
}
int main()
{
string num = "1234" ;
cout << sumOfSubstrings(num) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
static int toDigit( char ch) { return (ch - '0' ); }
static int sumOfSubStrings(String num)
{
int n = num.length();
int prev = toDigit(num.charAt( 0 ));
int res = prev;
int current = 0 ;
for ( int i = 1 ; i < n; i++) {
int numi = toDigit(num.charAt(i));
current = (i + 1 ) * numi + 10 * prev;
res += current;
prev = current;
}
return res;
}
public static void main(String[] args)
{
String num = "1234" ;
System.out.print(sumOfSubStrings(num) + "\n" );
}
}
|
Python3
def sumOfSubstrings(num):
n = len (num)
prev = int (num[ 0 ])
res = prev
current = 0
for i in range ( 1 , n):
numi = int (num[i])
current = (i + 1 ) * numi + 10 * prev
res + = current
prev = current
return res
if __name__ = = '__main__' :
num = "1234"
print (sumOfSubstrings(num))
|
C#
using System;
class GFG {
static int toDigit( char ch) { return (ch - '0' ); }
static int sumOfSubStrings( string num)
{
int n = num.Length;
int prev = toDigit(num[0]);
int res = prev;
int current = 0;
for ( int i = 1; i < n; i++) {
int numi = toDigit(num[i]);
current = (i + 1) * numi + 10 * prev;
res += current;
prev = current;
}
return res;
}
static void Main()
{
string num = "1234" ;
Console.WriteLine(sumOfSubStrings(num));
}
}
|
Javascript
<script>
function toDigit(ch)
{
return (ch - '0' );
}
function sumOfSubStrings(num)
{
let n = num.length;
let prev = toDigit(num[0]);
let res = prev;
let current = 0;
for (let i = 1; i < n; i++)
{
let numi = toDigit(num[i]);
current = (i + 1) * numi + 10 * prev;
res += current;
prev = current;
}
return res;
}
let num = "1234" ;
document.write(sumOfSubStrings(num) + "\n" );
</script>
|
Time complexity: O(N)
Auxiliary Space: O(1)
Related Article: 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 write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks