Sum of all substrings of a string representing a number | Set 1 | Returns sum of all substring of num ; allocate memory equal to length of string ; initialize first value with first digit ; loop over all digits of string ; update each sumofdigit from previous value ; add current value to the result ; Driver code
function sumOfSubstrings ( num ) { let n = num . length ; let sumofdigit = new Array ( n ) ; sumofdigit [ 0 ] = num [ 0 ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ; let res = sumofdigit [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { let numi = num [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ; sumofdigit [ i ] = ( i + 1 ) * numi + 10 * sumofdigit [ i - 1 ] ; res += sumofdigit [ i ] ; } return res ; } let num = " " ; document . write ( sumOfSubstrings ( num ) ) ;