Given an integer represented as a string, we need to get the sum of all possible substrings of this string.
Input : num = “1234” Output : 1670 Sum = 1 + 2 + 3 + 4 + 12 + 23 + 34 + 123 + 234 + 1234 = 1670 Input : num = “421” Output : 491 Sum = 4 + 2 + 1 + 42 + 21 + 421 = 491
We can solve this problem by using dynamic programming. We can write a summation of all substrings on basis of the digit at which they are ending in that case,
Sum of all substrings = sumofdigit + sumofdigit + sumofdigit … + sumofdigit[n-1] where n is length of string.
Where sumofdigit[i] stores the sum of all substring ending at ith index digit, in the above example,
Example : num = "1234" sumofdigit = 1 = 1 sumofdigit = 2 + 12 = 14 sumofdigit = 3 + 23 + 123 = 149 sumofdigit = 4 + 34 + 234 + 1234 = 1506 Result = 1670
Now we can get the relation between sumofdigit values and can solve the question iteratively. Each sumofdigit can be represented in terms of previous value as shown below,
For above example, sumofdigit = 4 + 34 + 234 + 1234 = 4 + 30 + 4 + 230 + 4 + 1230 + 4 = 4*4 + 10*(3 + 23 +123) = 4*4 + 10*(sumofdigit) In general, sumofdigit[i] = (i+1)*num[i] + 10*sumofdigit[i-1]
Using the above relation we can solve the problem in linear time. In the below code a complete array is taken to store sumofdigit, as each sumofdigit value requires just the previous value, we can solve this problem without allocating the complete array also.
Time Complexity: O(n) where n is the length of the input string.
Auxiliary Space: O(n)
Sum of all substrings of a string representing a number | Set 2 (Constant Extra Space)
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
O(1) space approach
The approach is the same as described above. What we have observed that at the current index we are dependent on the current sum + previous index sum so instead of storing in a dp array we can store it in two variables current and prev.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Sum of all substrings of a string representing a number | Set 2 (Constant Extra Space)
- Generate a string whose all K-size substrings can be concatenated to form the given string
- Lexicographically smallest permutation of a string that contains all substrings of another string
- Count the number of vowels occurring in all the substrings of given string
- Find the starting indices of the substrings in string (S) which is made by concatenating all words from a list(L)
- Minimum changes to a string to make all substrings distinct
- Lexicographical concatenation of all substrings of a string
- Lexicographically all Shortest Palindromic Substrings from a given string
- Count of substrings of a given Binary string with all characters same
- Check if all substrings of length K of a Binary String has equal count of 0s and 1s
- Lexicographic rank of a string among all its substrings
- Program to print all substrings of a given string
- Calculate Sum of ratio of special characters to length of substrings of the given string
- Split a given string into substrings of length K with equal sum of ASCII values
- Count substrings that contain all vowels | SET 2
- Check whether all the substrings have number of vowels atleast as that of consonants
- Given a binary string, count number of substrings that start and end with 1.
- Number of substrings divisible by 6 in a string of integers
- Number of even substrings in a string of digits
- Number of substrings with odd decimal value in a binary string