FastPrepCount Digit Replacements with No Equal Adjacent Digits

Count Digit Replacements with No Equal Adjacent Digits

Microsoft logoMicrosoft● MediumNEW GRADINTERNOA
Learn

Problem statement

You are given a string s containing digits from 0 through 9 and question marks.

Replace every ? with one digit so that no two adjacent digits are equal. Return the number of valid replacements modulo 10^9 + 7.

Function

getNumOfWays(s: String) → int

Examples

Example 1

s = "1?3?"return = 72

The first question mark may be any digit except 1 and 3, giving 8 choices. The last may be any digit except 3, giving 9 choices. Therefore the result is 8 * 9 = 72.

Example 2

s = "??"return = 90

The first position has 10 choices, and the second has 9 choices different from the first.

Constraints

  • 1 <= s.length <= 10^5
  • s contains only digits from 0 through 9 and ?.

More Microsoft problems

See Microsoft hiring insights
public int getNumOfWays(String s) {
  // write your code here
}
s"1?3?"
expected72
Checking account…