#C5945. Merge Groups with Unique Capabilities
Merge Groups with Unique Capabilities
Merge Groups with Unique Capabilities
You are given a list of groups. Each group can either be a Simple group containing an array of alphanumeric capabilities or a Compound group which nests other groups (either Simple or Compound). Your task is to merge all these groups into a single Simple group such that every capability appears only once. The final list of capabilities must be sorted in lexicographical order.
The nested structure can be arbitrarily deep. In formal terms, if we denote the set of capabilities from a group by \(C\), then the result should be \(\bigcup C\) over all groups, with duplicates removed and sorted. Input is provided in JSON format from standard input (stdin) and your merged group must be output as a JSON object on standard output (stdout).
inputFormat
The input consists of a single line containing a JSON array. Each element in the array is an object representing a group. A group has either a "Simple" key with an array of capabilities or a "Compound" key with an array of nested groups. Read the input from standard input (stdin).
outputFormat
Output a JSON object with a single key "Simple" and its value being the lexicographically sorted array of unique capabilities merged from all groups. Write the output to standard output (stdout).## sample
[
{ "Simple": ["read", "write"] },
{ "Compound": [
{ "Simple": ["execute"] },
{ "Compound": [
{ "Simple": ["delete", "write"] },
{ "Simple": ["read", "upload"] }
] }
] }
]
{"Simple": ["delete", "execute", "read", "upload", "write"]}