Mutual Wishlist Rankings
Problem statement
Users of an apartment-exchange service keep ordered wishlists of other users' apartments.
Each wishlist row is user:choice1,choice2,..., with rank 0 as the first choice. Process two kinds of operations:
MUTUAL user rank: returntruewhen the apartment at that rank belongs to another user who also placesuserat the same rank.CHANGED user rank:rankis at least1. Imagine moving that entry up one position by swapping ranksrankandrank-1. Without mutating the stored wishlist, return the users whose mutual-ranked status withuserwould change.
Return one string per operation. A changed list is formatted as sorted usernames inside brackets, for example [a,c] or [].
Function
evaluateWishlist(wishlists: String[], operations: String[]) → String[]Examples
Example 1
wishlists = ["a:c,d","b:d,a,c","c:a,b","d:c,a,b"]operations = ["MUTUAL a 0","MUTUAL b 0","MUTUAL a 1","CHANGED d 1","CHANGED b 2","CHANGED b 1"]return = ["true","false","true","[a]","[c]","[]"]a/c are mutual first choices, a/d are mutual second choices, and only the source-described pairings change under each hypothetical rank bump.
Example 2
wishlists = ["amy:bo,cy","bo:cy,amy","cy:amy,bo"]operations = ["MUTUAL amy 0","MUTUAL amy 1","CHANGED amy 1"]return = ["false","false","[bo,cy]"]Neither original rank is mutual. Swapping amy's choices creates mutual rank 0 with cy and mutual rank 1 with bo.
Constraints
1 <= wishlists.length <= 1000.- Usernames are unique, and every listed choice names another supplied user at most once.
1 <= operations.length <= 10000.- A
MUTUALrank may be out of range and then returnsfalse. - A
CHANGEDrank is valid and at least1. - Operations do not mutate the wishlists.