Algorithm · ByteDance · Hard
You are given a string N representing a positive integer (without leading zeros) and a set of distinct decimal digits A (each between 0 and 9). Construct the greatest possible integer that is strictly less than N and consists only of digits from A. Each digit may be reused arbitrarily many times. The constructed integer must not contain leading zeros – its first digit cannot be '0' unless the entire number is exactly 0. If no valid integer exists, return the string "-1".…