Equivalent Symbol Trades
MediumUnion Find · Bloomberg
The same instrument can trade under several ticker symbols (for example "GOOG" and "GOOGL" on different feeds). You are given:
pairs: each[a, b]says symbolsaandbare aliases of the same instrument. Aliasing is transitive: ifAis an alias ofBandBofC, thenA,BandCall form one group. A symbol that appears in no pair is a group by itself.trades: each[symbol, quantity]is a trade, wherequantityis an integer and may be negative (a correction).
The canonical symbol of a group is its lexicographically smallest symbol (plain string comparison), counting every symbol in the group, including ones that never trade.
Return one [canonical_symbol, total_quantity] entry for each group that has at least one trade, where total_quantity is the sum of the quantities of all trades on any symbol in that group. Sort the result by canonical symbol in ascending order. A group whose trades sum to 0 is still included.
Example 1
- Input:
- pairs = [["GOOG", "GOOGL"], ["GOOGL", "GOOG.O"], ["BRK.A", "BRK.B"]], trades = [["GOOGL", 100], ["GOOG.O", -20], ["BRK.B", 5], ["AAPL", 7], ["GOOG", 10]]
- Output:
- [["AAPL", 7], ["BRK.A", 5], ["GOOG", 90]]
- Explanation
GOOG,GOOGLandGOOG.Oform one group, andGOOGis its smallest symbol, so 100 - 20 + 10 = 90 is reported underGOOG. OnlyBRK.Btraded, but the group's canonical symbol isBRK.A.AAPLhas no aliases.
Example 2
- Input:
- pairs = [["B", "C"], ["A", "C"]], trades = [["B", 4], ["C", -4]]
- Output:
- [["A", 0]]
- Explanation
A,BandCare one group throughC.Anever trades but is the smallest symbol, and the total of 0 is still reported.
Example 3
- Input:
- pairs = [["A", "B"]], trades = []
- Output:
- []
- Explanation
- There are no trades, so no group is reported.
Constraints
0 <= pairs.length, trades.length <= 10^5- Symbols are non-empty strings of uppercase and lowercase letters, digits and
.; comparison is case-sensitive. -10^9 <= quantity <= 10^9