catalog and a list of queries queries. For each query queries[i], find the word in catalog that shares the longest common ending (suffix) with it. If several words tie, choose the shortest of them, and if the length ties too, the one that comes earlier in catalog.
The common ending may be empty, in which case every catalog word competes.
Return an array whose i-th element is the index in catalog of the word chosen for queries[i].catalog=["station", "nation", "ion"], queries=["motion", "nation", "cat"] → [1, 1, 2]
motion shares the ending tion with both station and nation; the shorter nation wins (index 1). nation matches nation completely (1). cat shares no ending with anything, so every word ties and the shortest, ion, wins (2).
catalog=["dog", "log", "fog"], queries=["frog", "dg"] → [0, 0]
Every catalog word ties on both the shared ending and the length, so the earliest one, index 0, wins each time.
catalog have total length at most 5 · 10⁵, and so do the words in queriescatalog=["station", "nation", "ion"], queries=["motion", "nation", "cat"]
[1, 1, 2]
Run checks the first 3 tests in your browser. Submit checks all 81 tests on the server, hidden ones included.