Homo or Hetero?

Interview

Time limit3sMemory limit256 MB

Summary
Maintain a list under insert and delete-first-occurrence operations and classify it after each step as homogeneous, heterogeneous, both, or neither.
Level

Medium4 of 10

Topics
Hash map, Implementation, Simulation
Solved
No attempts yet

Problem

You maintain a list of numbers that starts out empty and supports two operations:

  • insert number — appends the given number to the end of the list.
  • delete number — removes the first occurrence of the given number. If the number is not in the list, the list is left unchanged.

For example, inserting 44 into the list [1,2,1][1, 2, 1] gives [1,2,1,4][1, 2, 1, 4]. Deleting 11 from [1,2,1,4][1, 2, 1, 4] gives [2,1,4][2, 1, 4], while deleting 33 from [1,2,1,4][1, 2, 1, 4] leaves it unchanged.

A list is homogeneous if it contains at least two equal numbers, and heterogeneous if it contains at least two different numbers. For example, [2,2][2, 2] is homogeneous, [2,1,4][2, 1, 4] is heterogeneous, [1,2,1,4][1, 2, 1, 4] is both, and the empty list is neither.

Starting from the empty list, process a sequence of insert and delete operations and, after each one, report whether the list is homogeneous, heterogeneous, both, or neither.

Input

The first line contains an integer nn, the number of operations (1≤n≤100 0001 \le n \le 100\,000).

Each of the next nn lines describes one operation: the word insert or delete, followed by an integer kk (−109≤k≤109-10^9 \le k \le 10^9).

Output

After each operation, print one line describing the state of the list:

  • both — the list is both homogeneous and heterogeneous.
  • homo — the list is homogeneous but not heterogeneous.
  • hetero — the list is heterogeneous but not homogeneous.
  • neither — the list is neither homogeneous nor heterogeneous.

Examples2

  1. Example 1

    Input
    11
    insert 1
    insert 2
    insert 1
    insert 4
    delete 1
    delete 3
    delete 2
    delete 1
    insert 4
    delete 4
    delete 4
    
    Expected output
    neither
    hetero
    both
    both
    hetero
    hetero
    hetero
    neither
    homo
    neither
    neither
    
  2. Example 2

    Input
    4
    insert 7
    insert 7
    insert 5
    delete 5
    
    Expected output
    neither
    homo
    both
    homo