Shortest Currency Conversion Chain
Problem statement
Each row in pairs lists two currencies with a direct conversion and may be traversed in either direction. Return a chain from source to target using the fewest edges.
Break equal-hop ties by lexicographically comparing the complete currency sequences. Return an empty array when disconnected.
Function
shortestConversionChain(pairs: String[][], source: String, target: String) → String[]Examples
Example 1
pairs = [["USD","CAD"],["CAD","MEX"],["USD","EUR"],["EUR","MEX"]]source = "USD"target = "MEX"return = ["USD","CAD","MEX"]Both routes use two edges; the CAD route is lexicographically smaller.
Constraints
- Currency names are nonempty strings.
- Pairs contain no self edges.