Shortest Path Through Numbers Sharing a Digit
Problem statement
You are given an array of distinct non-negative integers numbers and two values start and end that appear in the array.
Two values are adjacent when their ordinary decimal representations share at least one digit. Find a path from start to end using these adjacencies.
Return a path with the minimum number of edges. Among several shortest paths, return the lexicographically smallest value sequence. Return an empty array when no path exists. The decimal representation of zero contains digit 0.
Function
shortestCommonDigitPath(numbers: int[], start: int, end: int) → int[]Examples
Example 1
numbers = [1,20,40,41]start = 1end = 20return = [1,41,40,20]Each consecutive pair shares digit 1, 4, and 0 respectively.
Example 2
numbers = [12,23,34,45]start = 12end = 45return = [12,23,34,45]The chain shares digits 2, 3, and 4.
Example 3
numbers = [10,22,35]start = 10end = 35return = []No pair in the array shares a digit.
Constraints
1 <= numbers.length <= 200000.0 <= numbers[i] <= 10^9, and all values are distinct.startandendappear innumbers.