#K3196. Count Items in Nested Storage

    ID: 24905 Type: Default 1000ms 256MiB

Count Items in Nested Storage

Count Items in Nested Storage

You are given a nested storage structure represented as a JSON array. Each element of the array is either a string (an item) or another array (a nested storage). Your task is to count the total number of items (strings) in the storage structure.

The input will be provided as a single line containing a valid JSON array. The array may include other arrays nested to any depth. You must output the total count of items found.

Here is the recurrence relation for the problem: \(T(n) = T(n_1) + T(n_2) + \cdots + T(n_k) + c\), where \(c\) is the constant time to process a single string element, and each \(T(n_i)\) corresponds to the count in a nested array.

inputFormat

The input is a single line containing a valid JSON array. Each element is either a string representing an item, or another nested JSON array.

Examples:

["item1", "item2", "item3"]
["item1", ["item2", ["item3", "item4"], "item5"], "item6"]

outputFormat

Output a single integer representing the total count of items in the nested storage structure. The output should be printed to stdout.

For example, given the input:

["item1", ["item2", ["item3", "item4"], "item5"], "item6"]

The correct output is:

6
## sample
["item1", "item2", "item3"]
3