Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Sum of all substrings of a number

Dp solution:
def sumSubstrings(self,s):
        n = len(s)
        dp = [0] * (n)
        dp[0] = int(s[0])
        res = int(s[0])
        for i in range(1, n):
            dp[i] = (i+1) * int(s[i]) + 10 * (dp[i-1])
            res += dp[i]
        return res % (10**9+7)
Comment

Sum of all substrings of a number


//User function Template for Java

class Solution
{
    //Function to find sum of all possible substrings of the given string.
    public static long sumSubstrings(String s)
    {
    long sum=0;
    long mod=1000000007;
    int count=s.length();
    long one=1;
    while(count>0)
    {
       long number=((s.charAt(count-1)-'0')*count)%mod;
       long temp=(one*number)%mod;
       //System.out.println(temp);
       sum=(sum+temp)%mod;
       one=(one*10+1)%mod;
       count--;
    }
    return sum;
    
    }
}
    /*1111*count*number 
 

    222*2
    
  
    33*3
    


     4*count*/
Comment

PREVIOUS NEXT
Code Example
Python :: split string by special characters python 
Python :: tic tac toe pygame 
Python :: python using end keyword 
Python :: make virtual environment python 
Python :: create a flask app 
Python :: python set workspace dir 
Python :: python last column of array 
Python :: python running mean pandas 
Python :: numpy unique axis 
Python :: get hours from datetime.timedelta in python (Django) 
Python :: open file in os python 
Python :: newsapi 
Python :: run a for loop in python 
Python :: string remove suffix python 
Python :: python types of loops 
Python :: check dictionary values pandas dataframe colu 
Python :: python count elements in sublists 
Python :: Generate bootstrap sample 
Python :: pahtlib join path 
Python :: sorted python 
Python :: numpy arange 
Python :: combine picture and audio python 
Python :: python skip input 
Python :: pandas dataframe apply 
Python :: df mask 
Python :: copy array along axis numpy 
Python :: Python script to SSH to server and run command 
Python :: serialize list to json python 
Python :: for i in array in range python 
Python :: remove punctuation from a string 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =