Minimum Currency Conversions
Problem statement
Each row in pairs contains two currency codes that can be converted directly in either direction.
Return the minimum number of direct conversions needed to move from source to target. Return -1 when no conversion chain exists. If the two codes are equal, return 0.
Function
minimumConversions(pairs: String[][], source: String, target: String) → intExamples
Example 1
pairs = [["USD","EUR"],["EUR","JPY"],["USD","GBP"]]source = "USD"target = "JPY"return = 2The shortest chain is USD to EUR to JPY.
Example 2
pairs = [["A","B"],["C","D"]]source = "A"target = "D"return = -1The currencies are in disconnected components.
Constraints
0 <= pairs.length <= 10000.- Every row contains two nonempty currency codes.
- Codes contain uppercase English letters.
Source note: Author-written prompt excerpt retained for source fidelity.