# Hackerrank: Sam and Substrings Solution

1function substrings(n) {2 const arr = []3 n.split("").forEach((d, i) => {4 if (i === 0) arr[i] = Number(d)5 else {6 arr[i] = ((i + 1) * Number(d) + 10 * arr[i - 1]) % (Math.pow(10, 9) + 7)7 }8 })9 return arr.reduce((a, b) => a + b, 0) % (Math.pow(10, 9) + 7)10}

## Comments

Loading comments...

## Tags

hackerrank

dynamic programming

## Apply and earn a $2,500 bonus once you're hired on your first job!

Clients from the Fortune 500 to Silicon Valley startups

Choose your own rate, get paid on time

From hourly, part-time, to full-time positions

Flexible remote working environment

A lot of open JavaScript jobs!!

**Fact corner:** Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.

**Still hesitate?** Read HoningJS author's guide on dealing with Toptal interview process.

## Next Post

Hackerrank: Pangrams

Jan 22, 2021

## Previous Post

Hackerrank: Simple Array Sum

Jan 22, 2021