267. Complete Weekly Work Hours
Complete Weekly Work Hours
A worker's daily hours for one week are represented by a seven-character string workHours. Each position corresponds to one day and contains either a digit from 0 to 9 or the character #.
Replace every # with a digit from 0 to 9 so that the sum of all seven digits is exactly requiredHours. Return all completed strings that satisfy this condition.
The returned strings must be arranged in lexicographically increasing order. Return an empty list when no valid replacement is possible.

Method Signature

List<String> completeWorkHours(String workHours, int requiredHours)
  • workHours represents the worker's hours for the seven days of the week.
  • requiredHours is the required sum of the seven daily values.
  • The method returns every valid completed weekly string in lexicographically increasing order.

Completion Rules

  • Each # must be independently replaced with one digit from 0 to 9.
  • Digits already present in workHours must remain unchanged.
  • Each digit represents the hours worked on its corresponding day.
  • A completed string is valid only when the sum of its seven digits equals requiredHours.
  • Zero may be used as a replacement, including at the beginning of the string.

Constraints

  • workHours.length() == 7
  • Every character in workHours is either '#' or a digit from '0' to '9'.
  • 0 ≤ requiredHours ≤ 63
  • workHours contains at least one character.
  • The output must be sorted in lexicographically increasing order.

Examples

Example 1

completeWorkHours( workHours = "#120#30", requiredHours = 10)
Output: ["0120430", "1120330", "2120230", "3120130", "4120030"]
The fixed digits contribute 6 hours, so the two missing digits must have a combined value of 4.

Example 2

completeWorkHours( workHours = "1111111", requiredHours = 7)
Output: ["1111111"]
There are no missing digits, and the existing seven digits already add up to 7.

Example 3

completeWorkHours( workHours = "9#90000", requiredHours = 10)
Output: []
The fixed digits already contribute 18 hours, which is greater than the required total.

Example 4

completeWorkHours( workHours = "000#000", requiredHours = 5)
Output: ["0005000"]
The only missing digit must be replaced with 5 to produce the required total.


Please use Laptop/Desktop or any other large screen to add/edit code.