HackerRank Sam and substrings Problem Solution Yashwant Parihar, June 13, 2023August 1, 2024 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.Examplen = ’42’Here n is a string that has 3 integer substrings: 4. 2, and 42. Their sum is 48, and48 modulo (10 power 9 +7)= 48.Function DescriptionComplete the substrings function in the editor below.substrings has the following parameter(s):string n: the string representation of an integerReturnsint: the sum of the integer values of all substrings in n. modulo 10 power 9 + 7Input FormatA single line containing an integer as a string, without leading zeros.Sample Input 016 Sample Output 023 Explanation 0The substrings of 16 are 16, 1 and 6 which sum to 23.Sample Input 1123 Sample Output 1164 Explanation 1The substrings of 123 are 1, 2, 3, 12, 23, 123 which sum to 164.HackerRank Sam and substrings Problem SolutionSam 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 Solutionusing 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 Solutionimport 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 Solutionvar 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 CcppCSharpHackerrank Solutionsjavajavascriptpython