Problem · Dynamic Programming

Good Strings

Learn this problem
MediumMathWorksOA

Problem statement

You are given two integers n and d. Returns the numbers of Good Strings having length n and absolute difference between the adjacent characters 'a' to 'z' is not more than d.

a and z have absolute difference 25. b and c have absolute difference 1.

Function

goodStrings(n: int, d: int) → int

GoodStrings has the following parameters:

  1. n: The size of the string.
  2. d: The difference.

Returns

int number of Good Strings mod 10^9 + 7.

Examples

Example 1

n = 2d = 2return = 124

There are 124 Good Strings of length 2 where the absolute difference between adjacent characters is not more than 2.

Example 2

n = 3d = 5return = 2596

There are 2596 Good Strings of length 3 where the absolute difference between adjacent characters is not more than 5.

Constraints

  • n ≤ 105
  • d ≤ 25

More MathWorks problems

drafts saved locally
public int goodStrings(int n, int d) {
  // write your code here
}
n2
d2
expected124
checking account