24. Sum of All Substrings of a Number

The problem can be found at the following link: ๐Ÿ”— Question Link

๐Ÿงฉ Problem Description

Given a string s representing an integer number, return the sum of all possible substrings of this string interpreted as integers.

Each substring is formed by selecting a continuous sequence of characters from the original string. You must return the sum of all such substrings.

๐Ÿ’ก The number may contain leading zeros and the result will always fit in a 32-bit signed integer.

๐Ÿ“˜ Examples

Example 1:

Input:

s = "6759"

Output:

8421

Explanation:

Substrings: 6, 7, 5, 9, 67, 75, 59, 675, 759, 6759

Sum = 6 + 7 + 5 + 9 + 67 + 75 + 59 + 675 + 759 + 6759 = 8421

Example 2:

Input:

s = "421"

Output:

491

Explanation:

Substrings: 4, 2, 1, 42, 21, 421

Sum = 4 + 2 + 1 + 42 + 21 + 421 = 491

๐Ÿ”’ Constraints

  • $1 \leq |s| \leq 9$

โœ… My Approach

One-Pass Dynamic Contribution

The idea is to use dynamic programming to compute the cumulative contribution of each digit to all substrings ending at its position.

Let f(i) be the sum of all substrings ending at index i. Then we observe that:

  • f(0) = int(s[0])

  • f(i) = f(i-1) * 10 + (i+1) * int(s[i])

We maintain a running total of the result and update the current contribution using the above formula.

Algorithm Steps:

  1. Initialize res = 0 and f = 0

  2. Traverse the string from index 0 to n-1:

    • Convert s[i] to integer and update: f = f * 10 + (i + 1) * digit

    • Add f to the result.

  3. Return res.

๐Ÿงฎ Time and Auxiliary Space Complexity

  • Expected Time Complexity: O(n), as we iterate through the string once and perform constant-time operations at each step.

  • Expected Auxiliary Space Complexity: O(1), as we only use a fixed number of variables (res, f, and loop index), regardless of input size.

๐Ÿง‘โ€๐Ÿ’ป Code (C++)

class Solution {
  public:
    int sumSubstrings(string &s) {
        long long res=0,f=0;
        for(int i=0;i<s.size();++i){
            f=f*10+(s[i]-'0')*(i+1);
            res+=f;
        }
        return (int)res;
    }
};
โšก Alternative Approaches

๐Ÿ“Š 2๏ธโƒฃ Two-Pointer Accumulation (O(nยฒ))

Instead of the one-pass DP, build each substring ending at i by extending with the current digit:

Algorithm Steps:

  1. Initialize res = 0.

  2. For each ending index i from 0 to n-1:

    • Let num = 0.

    • For j from i down to 0:

      • num = num + (s[j]-'0') * pow(10, i-j)

      • Add num to res.

  3. Return res.

class Solution {
  public:
    int sumSubstrings(string &s) {
        long long res=0;
        for(int i=0;i<s.size();++i){
            long long num=0, p=1;
            for(int j=i;j>=0;--j){
                num += (s[j]-'0') * p;
                p *= 10;
                res += num;
            }
        }
        return (int)res;
    }
};

โœ… Why This Approach?

  • Simpler to visualize each substring build-up.

  • No extra DP array or variables beyond local ones.

๐Ÿ“ Complexity Analysis:

  • Time: O(nยฒ) โ€” due to nested iteration.

  • Auxiliary Space: O(1) โ€” no additional memory used.

๐Ÿ“Š 3๏ธโƒฃ Brute-Force Substrings (O(nยณ))

Generate every substring, convert to integer, and sum:

Algorithm Steps:

  1. Initialize res = 0.

  2. For each start i and end j (i โ‰ค j):

    • Extract substring s.substr(i, j-i+1).

    • Convert to integer and add to res.

  3. Return res.

class Solution {
  public:
    int sumSubstrings(string &s) {
        long long res=0;
        for(int i=0;i<s.size();++i)
            for(int j=i;j<s.size();++j)
                res += stoll(s.substr(i,j-i+1));
        return (int)res;
    }
};

โœ… Why This Approach?

  • Direct and trivial implementation.

  • Useful for validating small inputs during testing.

๐Ÿ“ Complexity Analysis:

  • Time: O(nยณ) โ€” triple nested work due to substring creation and conversion.

  • Auxiliary Space: O(n) per substring.

๐Ÿ†š Comparison of Approaches

Approach

โฑ๏ธ Time

๐Ÿ—‚๏ธ Space

โœ… Pros

โš ๏ธ Cons

๐Ÿฅ‡ One-Pass DP

๐ŸŸก O(n)

๐ŸŸข O(1)

Fastest, minimal memory

Requires insight into contribution

โžก๏ธ Accumulation (nested)

๐Ÿ”ถ O(nยฒ)

๐ŸŸข O(1)

Easy to understand substring build-up

Slower for large n

๐Ÿ” Brute-Force

๐Ÿ”ด O(nยณ)

๐Ÿ”ถ O(n)

Simplest conceptual method

Impractical beyond small n

โœ… Best Choice by Scenario

Scenario

Recommended Approach

๐Ÿ† Fastest solution for any valid input

๐Ÿฅ‡ One-Pass DP

๐Ÿ“š Educational and clear logic

๐Ÿฅˆ Accumulation with Powers

๐Ÿงช Debugging or verifying small cases

๐Ÿฅ‰ Brute Force Substrings

๐Ÿง‘โ€๐Ÿ’ป Code (Java)

class Solution {
    public static int sumSubstrings(String s) {
        long res=0,f=0;
        for(int i=0;i<s.length();++i){
            f=f*10+(s.charAt(i)-'0')*(i+1);
            res+=f;
        }
        return (int)res;
    }
}

๐Ÿ Code (Python)

class Solution:
    def sumSubstrings(self,s):
        res=f=0
        for i,ch in enumerate(s):
            f=f*10+int(ch)*(i+1)
            res+=f
        return res

๐Ÿง  Contribution and Support

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: ๐Ÿ“ฌ Any Questions?. Letโ€™s make this learning journey more collaborative!

โญ If you find this helpful, please give this repository a star! โญ


๐Ÿ“Visitor Count

Last updated