Ctrl K

Letter Counts of Shortest Covers

Hard
You are given a list of distinct two-letter strings pairs. A string is a **cover** if every string from pairs occurs in it as a subsequence (its letters appear in order, though not necessarily next to each other). The **shortest covers** are the covers of the smallest possible length. Describe each shortest cover by its letter counts: an array of 26 numbers whose i-th entry says how many times the i-th letter of the English alphabet occurs in it. Covers that differ only in the order of their letters have the same counts. Return every distinct letter-count array of a shortest cover, in any order.
Example 1
pairs=["xy", "yx"] → [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0]]

The shortest covers are "xyx" and "yxy". The first has two x and one y, the second the other way round, so the letter counts differ.

Example 2
pairs=["ab", "bc"] → [[1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

The only shortest cover is "abc", with each letter once.

Example 3
pairs=["qq"] → [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

For "qq" to be a subsequence, two q letters are needed.

Constraints
  • 1 ≤ len(pairs) ≤ 256
  • Each string in pairs consists of two lowercase English letters
  • All strings in pairs are distinct
  • pairs uses at most 16 distinct letters in total
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLetter Order From a Dictionary HardTree Path Without Twin Neighbours HardSwapped Initials Names HardDishes You Can Cook MediumExpand a Template With Variables Medium
loading Python…
Input
pairs=["xy", "yx"]
Expected
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0]]

Run checks the first 3 tests in your browser. Submit checks all 120 tests on the server, hidden ones included.