Sum of all substrings of a string representing a number | Set 1 | 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 ; prev = current update previous ; Driver code to test above methods
def sumOfSubstrings ( num ) : NEW_LINE INDENT n = len ( num ) NEW_LINE prev = int ( num [ 0 ] ) NEW_LINE res = prev NEW_LINE current = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT numi = int ( num [ i ] ) NEW_LINE current = ( i + 1 ) * numi + 10 * prev NEW_LINE res += current NEW_LINE DEDENT return res NEW_LINE DEDENT num = "1234" NEW_LINE print ( sumOfSubstrings ( num ) ) NEW_LINE