#C735. Uniform JSON Checker
Uniform JSON Checker
Uniform JSON Checker
Given a JSON document as input, determine whether it is uniform.
A JSON document is defined as uniform if it is an object (i.e., a collection of key-value pairs) where all values are of the same type. In the case that the values are themselves JSON objects, they must have the same set of keys and, recursively, be uniform as well. An empty object is considered uniform.
For example:
- The document
{"a": "hello", "b": "world"}
is uniform, since both values are strings. - The document
{"a": 1, "b": 42}
is uniform, since both values are numbers. - The document
{"a": {"x": 1, "y": 2}, "b": {"x": 3, "y": 4}}
is uniform, because both nested objects have the same structure and uniformity. - The document
{"a": "hello", "b": 42}
is not uniform, as the value types differ. - The document
{"a": {"x": 1, "y": 2}, "b": {"x": 3, "z": 4}}
is not uniform, because the nested objects do not share the exact same set of keys.
In addition, note that the condition of uniformity can be mathematically expressed using the following criteria:
$$\text{Uniform}(J) = \begin{cases} \text{YES} & \text{if } J = \{\} \text{ or } \forall k_1,k_2,\, type(J[k_1]) = type(J[k_2]) \, \land \, (\text{if } type(J[k_1])=\text{Object} \text{ then } keys(J[k_1]) = keys(J[k_2]) \land Uniform(J[k_1])) \\ \text{NO} & \text{otherwise} \end{cases} $$inputFormat
The input consists of a single line containing a valid JSON document (string) representing an object.
outputFormat
Output a single line: YES
if the JSON document is uniform according to the definition above, or NO
otherwise.
{"a": "hello", "b": "world"}
YES