Imagine an access-control system where groups can contain both users and other groups. You are given three mappings:
groupChildren: a map from a group ID to the list of direct subgroup IDs inside it.groupUsers: a map from a group ID to the list of user IDs directly assigned to it.userDevices: a map from a user ID to the list of device IDs assigned to that user.Access starts from every ID listed in startGroups. For each accessible group, its direct users gain access, and the same process is repeated for all subgroups reachable through groupChildren. Cycles may exist in the group graph, so each group should be processed at most once. Every reachable user contributes every device ID from userDevices. Return the unique device IDs that become accessible, sorted lexicographically. A missing key in any mapping should be treated as an empty list.
Example 1:
Input:
groupChildren = {"R1": ["R2", "R3"], "R2": [], "R3": []}
groupUsers = {"R1": ["U10"], "R2": ["U11"], "R3": ["U12"]}
userDevices = {"U10": ["devA"], "U11": ["devB", "devC"], "U12": ["devD"]}
startGroups = ["R1"]
Output:
["devA", "devB", "devC", "devD"]
Explanation: Access expands from R1 into R2 and R3; the devices from U10, U11, and U12 are collected and returned once each.
group_children = {"R1": ["R2","R3"], "R2": [], "R3": []} group_users = {"R1": ["U10"], "R2": ["U11"], "R3": ["U12"]} user_devices = {"U10": ["devA"], "U11": ["devB", "devC"], "U12": ["devD"]} start_groups = ["R1"]
["devA","devB", "devC", "devD"]
Access starts at group R1. R1 contains subgroups R2 and R3.
Example 2:
Input:
groupChildren = {"P": ["Q"], "Q": ["R"], "R": ["P"]}
groupUsers = {"P": ["alice"], "Q": ["bob", "alice"], "R": ["carol"]}
userDevices = {"alice": ["tab", "phone"], "bob": ["phone", "watch"], "carol": []}
startGroups = ["P"]
Output:
["phone", "tab", "watch"]
Explanation: The cycle P -> Q -> R -> P is handled without infinite traversal, and duplicate user or device IDs are removed before sorting.
Example 3:
Input:
groupChildren = {"Z1": []}
groupUsers = {"Z1": ["u1"]}
userDevices = {}
startGroups = ["Z1"]
Output:
[]
Explanation: The only reachable user has no entry in userDevices, so no devices are accessible.
Constraints:
m is the total number of subgroup references, group-user memberships, and user-device assignments combined.group_children = {"R1": ["R2","R3"], "R2": [], "R3": []} group_users = {"R1": ["U10"], "R2": ["U11"], "R3": ["U12"]} user_devices = {"U10": ["devA"], "U11": ["devB", "devC"], "U12": ["devD"]} start_groups = ["R1"]
["devA","devB", "devC", "devD"]
Access starts at group R1. R1 contains subgroups R2 and R3.