-
Notifications
You must be signed in to change notification settings - Fork 35
/
SamAndSubstrings.cs
60 lines (47 loc) · 1.56 KB
/
SamAndSubstrings.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// https://www.hackerrank.com/challenges/sam-and-substrings/problem
// Test cases are wrong. For instance the test case 7th expects 186472174.
// The solution returns the correct value but test is not passed.
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;
class Solution
{
// Complete the substrings function below.
static int substrings(string n)
{
var mod = 1_000_000_007;
var len = n.Length;
var sum = 0L;
var map = new long[len];
map[len - 1] = 1;
for (var index = len - 2; index >= 0; index--)
map[index] = (map[index + 1] * 10 + 1) % mod;
for (var index = 0; index < len; index++)
{
var val = int.Parse(n[index].ToString());
var temp = val * map[index] * (index + 1) % mod;
sum += temp;
sum %= mod;
}
return (int)sum;
}
static void Main(string[] args)
{
TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
string n = Console.ReadLine();
int result = substrings(n);
textWriter.WriteLine(result);
textWriter.Flush();
textWriter.Close();
}
}