Save 25%Black Friday: 25% off all plans — use code BLACKFRIDAY2026 at checkout. Ends Monday, Dec 7.Claim the deal

Given a weekly work schedule represented as a 7-character string (called pattern), each character is either a digit (from '0' to '8') or a question mark ('?'). The digit indicates the number of hours worked on that day, while a '?' denotes a day for which the work hours are not yet assigned. An employee must work exactly workHours in a week. For every day that is unassigned ('?'), the maximum allowable work hours is given by dayHours.

Your task is to generate all possible valid schedules by replacing every '?' in pattern with a valid digit (ranging from 0 to dayHours) so that the sum of the scheduled hours equals workHours. The valid schedules should be returned in ascending lexicographical order.

Constraints:

  • pattern is exactly 7 characters long.
  • Each unassigned day ('?') can only take a value between 0 and dayHours.
  • Pre-assigned digits in pattern must remain unchanged.
  • Assume the solution always exists.

Example 1:

Input: workHours = 24, dayHours = 4, pattern = "08??840" Output: ["0804840", "0813840", "0822840", "0831840", "0840840"] Explanation: In the pattern "08??840", the fixed digits are at positions 0, 1, 4, 5, and 6, which sum up to 0 + 8 + 8 + 4 + 0 = 20 hours. The remaining two positions (marked by '?') must therefore sum up to 4 hours (i.e., 24 - 20 = 4). For each '?', valid replacements are digits from 0 to 4. The possible pairs that add up to 4 are: (0,4), (1,3), (2,2), (3,1), and (4,0). Inserting these pairs into the original string at the positions of '?' produces the output in ascending lexicographical order.

Example 2:

Input: workHours = 56, dayHours = 8, pattern = "???????" Output: ["8888888"]

Example 3:

Input: workHours = 3, dayHours = 2, pattern = "??2??00" Output: ["0020100", "0021000", "0120000", "1020000"]


Accepted
3k/6.3k
Acceptance Rate48.1%
Test Cases
Input
workHours =
dayHours =
pattern =
Output
Expected