04(April) Sum of all substrings of a number
04. Sum of All Substrings of a Number
The problem can be found at the following link: Question Link
Problem Description
Given an integer ( s ) represented as a string, the task is to get the sum of all possible sub-strings of this string. As the answer will be large, return answer modulo ( 10^9 + 7 ).
Example:
Input:
s = "1234"Output:
1670Explanation: Sum = 1 + 2 + 3 + 4 + 12 + 23 + 34 + 123 + 234 + 1234 = 1670
Your Task:
You only need to complete the function sumSubstrings that takes s as an argument and returns the answer modulo ( 10^9 + 7 ).
Constraints:
( 1 \leq |s| \leq 10^5 )
My Approach 1
Initialization:
Initialize variables
modto ( 10^9 + 7 ),rto 1, andresto 0.
Iterative Calculation:
Iterate from ( i = \text{size of } s - 1 ) to 0.
Within each iteration:
Update
resas ( (res + ((s[i] - '0') \times (i + 1) \times r) % \text{mod}) % \text{mod} ).Update
ras ( (r \times 10 + 1) % \text{mod} ).
Return:
Return
res.
Time and Auxiliary Space Complexity
Expected Time Complexity: ( O(|s|) ), as we iterate through the string
sonce.Expected Auxiliary Space Complexity: ( O(1) ), as we only use a constant amount of additional space.
Code 1 (C++)
My Approach 2
Initialization:
Initialize variables
modto ( 10^9 + 7 ),rto 1, andresto 0.
Iterative Calculation:
Iterate from ( i = \text{size of } s - 1 ) to 0.
Within each iteration:
Update
resas ( (res + ((s[i] - '0') \times (i + 1) \times r) % \text{mod}) % \text{mod} ).Update
ras ( (r \times 10 + 1) % \text{mod} ).
Return:
Return
res.
Time and Auxiliary Space Complexity
Expected Time Complexity: ( O(|s|) ), as we iterate through the string
sonce.Expected Auxiliary Space Complexity: ( O(1) ), as we only use a constant amount of additional space.
Code 2 (C++)
My Approach 3
Initialization:
Initialize variables
modto ( 10^9 + 7 ),resultto 0,multiplierto 1, andpositionSumto 0.
Iterative Calculation:
Iterate from ( i = \text{size of } s - 1 ) to 0.
Within each iteration:
Update
positionSumas ( (positionSum + (s[i] - '0') \times \text{multiplier}) % \text{mod} ).Update
resultas ( (result + \text{positionSum}) % \text{mod} ).Update
multiplieras ( (multiplier \times 10 + 1) % \text{mod} ).
Return:
Return
result.
Time and Auxiliary Space Complexity
Expected Time Complexity: ( O(|s|) ), as we iterate through the string
sonce.Expected Auxiliary Space Complexity: ( O(1) ), as we only use a constant amount of additional space.
Code 3 (C++)
My Approach 4
Initialization:
Initialize variables
modto ( 10^9 + 7 ),rto 1, andresto 0.
Iterative Calculation:
Iterate from ( i = \text{size of } s - 1 ) to 0.
Within each iteration:
Update
resas ( (res + ((s[i] - '0') \times (i + 1) \times r) % \text{mod}) % \text{mod} ).Update
ras ( (r \times 10 + 1) % \text{mod} ).
Return:
Return
res.
Time and Auxiliary Space Complexity
Expected Time Complexity: ( O(|
s|) ), as we iterate through the string s once.
Expected Auxiliary Space Complexity: ( O(1) ), as we only use a constant amount of additional space.
Code 4 (C++)
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