Increasing List
Time limit2sMemory limit128 MB
Replace every '?' in a string with a digit or comma to form the lexicographically smallest strictly increasing list of positive integers with no leading zeros, or print -1 if impossible.
- Level
Hard8 of 10
- Topics
- Backtracking, Greedy, String, Dynamic programming
- Solved
- No attempts yet
Problem
A string represents an increasing list if every comma-separated item is a positive integer, no item starts with 0, and each number is strictly larger than the one before it.
You are given a string consisting only of digits, commas, and question marks. Replace each question mark with either one digit or one comma so that the resulting string is an increasing list. If several results are possible, print the lexicographically smallest one. In this lexicographic order, the comma , comes before the digits 0 through 9.
Input
The first line contains a string made only of digits, commas, and question marks. Its length is at most 50.
Output
Print the lexicographically smallest possible resulting string. If it is impossible, print -1.