Sum of all substrings of a string representing a number | Set 1 | Utility method to convert character digit to integer digit ; Returns sum of all subString of num ; Storing prev value ; SubStrings sum upto current index loop over all digits of String ; Update each sumofdigit from previous value ; Add current value to the result ; Update previous ; Driver Code
function toDigit ( ch ) { return ( ch - ' ' ) ; } 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 = " " ; document . write ( sumOfSubStrings ( num ) + " " ) ;