Set!
InterviewTime limit1sMemory limit1024 MB
Given 12 Set cards written as four lines of three codes, list every trio where each of the four attributes is all the same or all different.
- Level
Easy3 of 10
- Topics
- Brute force, Implementation
- Solved
- No attempts yet
Problem

Set is a card game designed by Marsha Falco in 1974. Set Enterprises, Inc. markets the game, and it also appears in syndicated form on the website of the New York Times. The player is shown 12 cards (see the illustration), and each card has 1, 2, or 3 symbols. The symbols are diamonds, squiggles, or ovals. Each symbol is drawn with a solid, striped, or open fill style. Each symbol's color is red, green, or purple. All symbols on a given card have the same shape, color, and fill style.
To make a set, pick three cards for which each of the four characteristics is either the same on all three cards or different on all three cards. For example, take three cards: the first shows 2 striped red ovals, the second shows 3 striped green squiggles, and the third shows 1 striped purple diamond. The counts 2, 3, and 1 are all different. The shapes oval, squiggle, and diamond are all different. The colors red, green, and purple are all different. All three share the striped fill style. These three cards form a set.
Write a program that finds all sets among the 12 given cards.
Input
The input has 4 lines, each containing 3 strings that represent 3 cards. Each card is written as ABCD, where:
- A is 1, 2, or 3, the number of symbols.
- B is D, S, or O, for diamonds (D), squiggles (S), and ovals (O).
- C is S, T, or O, for solid (S), striped (T), and open (O) fill styles.
- D is R, G, or P, for red (R), green (G), and purple (P).
The cards are arranged as follows:
+----------+
| 1 2 3 |
| 4 5 6 |
| 7 8 9 |
| 10 11 12 |
+----------+
Output
Print every set you find, one per line. Print the card numbers of each set in sorted order. List the sets in sorted order by the number of their first card, breaking ties by the second and third card numbers. If no set can be formed, print no sets (without punctuation).