Approach #1: Hash Set [Accepted]

Intuition

If a card has the same value x on the front and back, it is impossible to win with x. Otherwise, it has two different values, and if we win with x, we can put x face down on the rest of the cards.

Algorithm

Remember all values same that occur twice on a single card. Then for every value x on any card that isn't in same, x is a candidate answer. If we have no candidate answers, the final answer is zero.

Complexity Analysis

  • Time Complexity: , where is the length of fronts (and backs). We scan through the arrays.

  • Space Complexity: .


Analysis written by: @awice.