Skip to content
  • Home
  • Contact Us
  • About Us
  • Privacy Policy
  • DMCA
  • Linkedin
  • Pinterest
  • Facebook
thecscience

TheCScience

TheCScience is a blog that publishes daily tutorials and guides on engineering subjects and everything that related to computer science and technology

  • Home
  • Human values
  • Microprocessor
  • Digital communication
  • Linux
  • outsystems guide
  • Toggle search form
HackerRank Sam and substrings Problem Solution

HackerRank Sam and substrings Problem Solution

Posted on June 13, 2023June 13, 2023 By Yashwant Parihar No Comments on HackerRank Sam and substrings Problem Solution

In this post, we will solve HackerRank Sam and substrings Problem Solution.

Samantha and Sam are playing a numbers game. Given a number as a string, no leading zeros, determine the sum of all integer values of substrings of the string.
Given an integer as a string, sum all of its substrings cast as integers. As the number may become large, return the value modulo 109 +7.
Example
n = ’42’
Here n is a string that has 3 integer substrings: 4. 2, and 42. Their sum is 48, and
48 modulo (10 power 9 +7)= 48.
Function Description
Complete the substrings function in the editor below.
substrings has the following parameter(s):
string n: the string representation of an integer
Returns
int: the sum of the integer values of all substrings in n. modulo 10 power 9 + 7
Input Format
A single line containing an integer as a string, without leading zeros.

Sample Input 0

16

Sample Output 0

23

Explanation 0

The substrings of 16 are 16, 1 and 6 which sum to 23.

Sample Input 1

123

Sample Output 1

164

Explanation 1

The substrings of 123 are 1, 2, 3, 12, 23, 123 which sum to 164.

HackerRank Sam and substrings Problem Solution
HackerRank Sam and substrings Problem Solution

Table of Contents

  • Sam and substrings Problem C Solution
  • Sam and substrings Problem C++ Solution
  • Sam and substrings Problem C Sharp Solution
  • Sam and substrings Problem Java Solution
  • Sam and substrings Problem JavaScript Solution
  • Sam and substrings Problem Python Solution

Sam and substrings Problem C Solution

#include<stdio.h>
#include<malloc.h>
#include<string.h>
#define mod 1000000007
int main()
{
	int i,j,n,k;
	char s[200001];
	scanf("%s",s);
	n=strlen(s);
	long long int val,count,ones;
	long long int a[200001];
	count=0;
	a[n]=0;
	ones=1;
	for(i=n-1;i>=0;i--)
	{
		//printf("%d*%lld+%lld\n",s[i]-'0',ones,a[i+1]);
		a[i]=((s[i]-'0')*(ones)+a[i+1])%mod;
		ones=(ones*10+1)%mod;
		count=(count+a[i])%mod;
	}
	printf("%lld\n",count);
	return 0;
}

Sam and substrings Problem C++ Solution

#include <cmath>
#include <cstdio>
#include <fstream>
#include <vector>
#include <iostream>
#include <algorithm>
#include <iomanip>
#define MOD 1000000007
using namespace std;



int len; 


void init();

int main() {
    
    init();
}

void init() {

 

    ios_base::sync_with_stdio(0);cin.tie(0);

    char in;
    long long int with_i = 0, with_ni = 0, last_i = 0, last_ni = 0, temp_i, temp_ni;
    int num ;
    int counter = 0;
    while( ((in =getchar()) != EOF) ){
        num = in-'0';

        with_ni = (with_ni + with_i)%MOD;
        with_i = ((with_i*10)%MOD + ((num)*(counter+1)%MOD))%MOD;
        //if(num){
            counter++;
        //}
    }
    cout<<(with_i+with_ni)%MOD<<endl;

    
 


    
}

Sam and substrings Problem C Sharp Solution

using System;
using System.Collections.Generic;
using System.IO;
class Solution {
    static void Main(String[] args) {
        string input = Console.ReadLine();
        double result = 0;
        double[] dp = new double[input.Length];
        for(int i = 0; i < input.Length; i++){
            double temp = Convert.ToDouble(input[i].ToString());
            dp[i] = (temp + i * temp + (i - 1 >= 0 ? 10 * dp[i - 1] : 0)) % 1000000007;
        }
        for(int i = 0; i < input.Length; i++){
            result = (result + dp[i]) % 1000000007;
        }
        Console.WriteLine(result % 1000000007);
    }
}

Sam and substrings Problem Java Solution

import java.io.*;
import java.util.*;
import java.lang.*;



class Solve{
    int mod = (int)Math.pow(10,9)+7;
    

    long totalSum(String s){
        long ans=0;
        int l = s.length();
        long [] a = new long[l+1];
        long [] b = new long[l+1];
        a[0] = 1;
        b[0] = 1;
        for(int i=1;i<l;i++){
            a[i] = (a[i-1]*10)%mod;
            b[i] = (b[i-1] +a[i])%mod ;
        }
        for(int i=0;i<l;i++){
            long temp = Integer.parseInt(s.substring(i,i+1));
            ans = (ans+(b[l-i-1]*temp*(i+1))%mod )%mod;
        }

        return ans;
    }
}


class Solution{
    public static void main(String[]args){
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        Solve slv = new Solve();
        long ans  = slv.totalSum(s);
        System.out.println(ans);   
    }
}

Sam and substrings Problem JavaScript Solution

var BigNumber = require('bignumber.js');

function processData(arr) {
    var sum = new BigNumber(0);
    var modulo = new BigNumber(1000000000).plus(7);

    for(var i = arr.length - 1, start = new BigNumber(1); i >= 0; i--) {
        if(arr[i] != '0') {
            var a = new BigNumber((arr[i] - '0') * (i + 1)).times(start);
        
            sum = sum.plus(a);
        }
        
        start = start.times(10).plus(1).modulo(modulo);
    }

    console.log(sum.modulo(modulo).toFixed());
}

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});

Sam and substrings Problem Python Solution

#!/usr/bin/env python

def main():
	Mod=10**9+7
	s=[0]+list(map(int,list(input())))
	#print(s)
	n=len(s)
	ans=0
	now=0
	for i in range(1,n):
		now=(now*10+s[i]*i)%Mod
		ans=(ans+now)
		if ans>=Mod:
			ans-=Mod
	print(ans)




if __name__=='__main__':
	main()
c, C#, C++, HackerRank Solutions, java, javascript, python Tags:C, cpp, CSharp, Hackerrank Solutions, java, javascript, python

Post navigation

Previous Post: HackerRank Yet Another KMP Problem Solution
Next Post: HackerRank Beautiful 3 Set Problem Solution

Related Posts

HackerRank Toll Cost Digits Problem Solution HackerRank Toll Cost Digits Problem Solution c
HackerRank Sherlock and Squares Problem Solution HackerRank Sherlock and Squares Solution c
HackerRank Service Lane Problem Solution HackerRank Service Lane Problem Solution c
HackerRank Drive Problem Solution HackerRank Drive Problem Solution c
HackerRank Prime Digit Sums Problem Solution HackerRank Prime Digit Sums Problem Solution c
HackerRank Hacker Country Problem Solution HackerRank Hacker Country Problem Solution c

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Pick Your Subject
Human Values

Copyright © 2023 TheCScience.

Powered by PressBook Grid Blogs theme