Sum of all substrings of a string representing a number | Set 2 (Constant Extra Space)
Given a string representing a number, we need to get the sum of all possible sub strings of this string.
Examples :
Input : s = "6759"
Output : 8421
sum = 6 + 7 + 5 + 9 + 67 + 75 +
59 + 675 + 759 + 6759
= 8421
Input : s = "16"
Output : 23
sum = 1 + 6 + 16 = 23
We have discussed a solution in below post.
Sum of all substrings of a string representing a number | Set 1
The solution is based on a different approach which does not use any extra space.
This problem can be viewed as follows.
Let number be s = “6759”
1 10 100 1000
6 1 1 1 1
7 2 2 2
5 3 3
9 4
The above table indicates that, when all the substrings are converted further to the ones, tens, hundreds etc.. form, each index of the string will have some fixed occurrence. The 1st index will have 1 occurrence each of ones, tens etc..The 2nd will have 2, 3rd will have 3 and so on.
One more point is that the occurrence of the last element will only be restricted to ones. Last 2nd element will be restricted to ones and tens. last 3rd will be up to a hundred and so on.
From the above points lets find out the sum.
sum = 6*(1*1 + 1*10 + 1*100 + 1*1000) + 7*(2*1 + 2*10 + 2*100) +
5*(3*1 + 3*10) + 9*(4*1)
= 6*1*(1111) + 7*2*(111) + 5*3*(11) + 9*4*(1)
= 6666 + 1554 + 165 + 36
= 8421
Now, to handle the multiplication we will be having a multiplying factor which starts from 1. It’s clear from the example that the multiplying factor(in reverse) is 1, 11, 111, … and so on. So the multiplication will be based on three factors. number, its index, and a multiplying factor.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int sumOfSubstrings(string num)
{
long long int sum = 0;
long long int mf = 1;
for ( int i=num.size()-1; i>=0; i--)
{
sum += (num[i]- '0' )*(i+1)*mf;
mf = mf*10 + 1;
}
return sum;
}
int main()
{
string num = "6759" ;
cout << sumOfSubstrings(num) << endl;
return 0;
}
|
Java
import java.util.Arrays;
public class GFG {
public static long sumOfSubstrings(String num)
{
long sum = 0 ;
long mf = 1 ;
for ( int i = num.length() - 1 ; i >= 0 ; i --)
{
sum += (num.charAt(i) - '0' ) * (i + 1 ) * mf;
mf = mf * 10 + 1 ;
}
return sum;
}
public static void main(String[] args)
{
String num = "6759" ;
System.out.println(sumOfSubstrings(num));
}
}
|
Python3
def sumOfSubstrings(num):
sum = 0
mf = 1
for i in range ( len (num) - 1 , - 1 , - 1 ):
sum = sum + ( int (num[i])) * (i + 1 ) * mf
mf = mf * 10 + 1
return sum
if __name__ = = '__main__' :
num = "6759"
print (sumOfSubstrings(num))
|
C#
using System;
public class GFG {
public static long sumOfSubstrings( string num)
{
long sum = 0;
long mf = 1;
for ( int i = num.Length - 1; i >= 0; i --)
{
sum += (num[i] - '0' ) * (i + 1) * mf;
mf = mf * 10 + 1;
}
return sum;
}
public static void Main()
{
string num = "6759" ;
Console.WriteLine(sumOfSubstrings(num));
}
}
|
PHP
<?php
function sumOfSubstrings( $num )
{
$sum = 0;
$mf = 1;
for ( $i = strlen ( $num ) - 1; $i >= 0; $i --)
{
$sum += ( $num [ $i ] - '0' ) * ( $i + 1) * $mf ;
$mf = $mf * 10 + 1;
}
return $sum ;
}
$num = "6759" ;
echo sumOfSubstrings( $num ), "\n" ;
?>
|
Javascript
<script>
function sumOfSubstrings(num)
{
let sum = 0;
let mf = 1;
for (let i = num.length - 1; i >= 0; i --)
{
sum += (num[i].charCodeAt() - '0' .charCodeAt()) * (i + 1) * mf;
mf = mf * 10 + 1;
}
return sum;
}
let num = "6759" ;
document.write(sumOfSubstrings(num));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Last Updated :
12 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...