Number of Balanced Strings
Updated
Problem statement
Count the balanced strings of length n over the lowercase English alphabet.
A string is balanced when the absolute difference between the alphabet positions of every pair of adjacent characters is at most d.
Return the count modulo 1,000,000,007.
Function
numberOfBalancedStrings(n: int, d: int) → intExamples
Example 1
n = 3d = 3return = 1134Dynamic counting over the final letter gives 1,134 balanced lowercase strings of length 3 when adjacent letters may differ by at most 3.
Example 2
n = 2d = 2return = 124There are 124 ordered pairs of lowercase letters whose alphabet-position difference is at most 2.
Constraints
1 <= n <= 10^60 <= d <= 25
More Moveworks interview questions
Similar questions at other companies
- Longest Palindromic SubsequenceAXQ Capital · OA
- Maximum Equal-Bit Pairs in a SubsequenceAmazon · OA
- Longest Subsequence That Is a SubstringBooking.com · OA
- Count Digit Replacements with No Equal Adjacent DigitsMicrosoft · OAMicrosoft interview questions
- Palindrome CounterGoldman Sachs · OAGoldman Sachs interview questions
- Longest String ChainVisa · OAVisa interview questions
- Minimum-Cost Digit String DecodingSquadStack.ai · OA
- Maximum Redundancy After One Segment FlipBloomberg LP · OABloomberg LP interview questions
- Substrings Without a Target SubsequencePhonePe · OAPhonePe interview questions
- Minimum Edit-Distance String for Each QueryTarget · OA