Approach #1: Hash Map [Accepted]

Intuition and Algorithm

The algorithm is straightforward: we just do what the problem statement tells us to do.

For an address like a.b.c, we will count a.b.c, b.c, and c. For an address like x.y, we will count x.y and y.

To count these strings, we will use a hash map. To split the strings into the required pieces, we will use library split functions.

Complexity Analysis

  • Time Complexity: , where is the length of cpdomains, and assuming the length of cpdomains[i] is fixed.

  • Space Complexity: , the space used in our count.

Analysis written by: @awice.