A test kitchen encodes each recipe card as a string of lowercase letters, where every letter represents one unit of a particular ingredient. A letter appearing twice in a card means the recipe calls for two units of that ingredient — for example, the card "aab" needs two units of ingredient a and one unit of ingredient b.
Given several recipe cards, find every ingredient unit that is common to all of the cards: a unit counts as common only if every single card contains at least that many units of the same letter. Each common unit must be listed as many times as it is guaranteed to appear across all of the cards.
Line 1: an integer n — the number of recipe cards. Next n lines: one lowercase string per line, the letters of that recipe card (1 <= length <= 100, only lowercase 'a'-'z', a card may repeat letters).
Print the common ingredient units as a single space-separated line, in non-decreasing alphabetical order (repeats of the same letter listed consecutively). If no unit is common to every card, print an empty line.
1 <= n <= 100 1 <= length of each card <= 100 every card contains only lowercase English letters
Example 1
Input
3 bella label roller
Expected
e l l
Explanation
Card 'bella' has a1 b1 e1 l2; card 'label' has a1 b1 e1 l2; card 'roller' has e1 l2 o1 r1. Taking the minimum count of each letter across all three cards gives e:1 and l:2, while a, b, o, and r drop to 0 because at least one card is missing them. Sorted alphabetically, the shared units are e, l, l.
Example 2
Input
2 cool lock
Expected
c l o
Explanation
Card 'cool' has c1 l1 o2; card 'lock' has c1 k1 l1 o1. The minimum of each letter across both cards is c:1, l:1, o:1, while k drops to 0 since 'cool' has none. Sorted alphabetically, the shared units are c, l, o.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →