Frequencies of Shortest Supersequences Problem
Description
LeetCode Problem 3435.
You are given an array of strings words. Find all shortest common supersequences (SCS) of words that are not permutations of each other.
A shortest common supersequence is a string of minimum length that contains each string in words as a subsequence.
Return a 2D array of integers freqs that represent all the SCSs. Each freqs[i] is an array of size 26, representing the frequency of each letter in the lowercase English alphabet for a single SCS. You may return the frequency arrays in any order.
Example 1:
Input: words = ["ab","ba"]
Output: [[1,2,0,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]]
Explanation:
The two SCSs are "aba" and "bab". The output is the letter frequencies for each one.
Example 2:
Input: words = ["aa","ac"]
Output: [[2,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]
Explanation:
The two SCSs are "aac" and "aca". Since they are permutations of each other, keep only "aac".
Example 3:
Input: words = ["aa","bb","cc"]
Output: [[2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]
Explanation:
"aabbcc" and all its permutations are SCSs.
Constraints:
1 <= words.length <= 256words[i].length == 2- All strings in
wordswill altogether be composed of no more than 16 unique lowercase letters. - All strings in
wordsare unique.
Solution
Approach: Feedback Vertex Sets over Bitmasks
Treat every letter as a vertex and every word xy as an edge x -> y. In a shortest supersequence each letter appears once or twice. Letters that appear twice can satisfy all their edges by putting one copy first and the other last. Letters that appear once must be orderable, so the graph induced on them must have no cycle (a word like aa is a cycle by itself).
So the shortest length is the number of letters plus the size of a smallest set S of letters whose removal leaves an acyclic graph, and every such minimum set S gives one frequency array (2 for letters in S, 1 for the others). Different sets give different arrays, and permutations of the same multiset are not repeated.
There are at most 16 letters, so we compute acyclic[mask] for every subset: a subset is acyclic if some vertex in it has no incoming edge from the subset and the rest is acyclic. Then we scan all sets S, keep those with the smallest size whose complement is acyclic, and output the arrays.
Complexity: O(2^m * m) time and O(2^m) space, where m <= 16 is the number of distinct letters.
class Solution:
def supersequences(self, words: List[str]) -> List[List[int]]:
letters = sorted(set("".join(words)))
m = len(letters)
idx = {c: i for i, c in enumerate(letters)}
pred = [0] * m
for w in words:
pred[idx[w[1]]] |= 1 << idx[w[0]]
# ok[mask]: the graph induced on mask has no cycle (and no self loop)
ok = [False] * (1 << m)
ok[0] = True
for mask in range(1, 1 << m):
for v in range(m):
if mask >> v & 1 and pred[v] & mask == 0 and ok[mask ^ (1 << v)]:
ok[mask] = True
break
full = (1 << m) - 1
best = m + 1
res = []
for s in range(1 << m):
if ok[full ^ s]:
c = bin(s).count("1")
if c < best:
best = c
res = [s]
elif c == best:
res.append(s)
ans = []
for s in res:
f = [0] * 26
for i, ch in enumerate(letters):
f[ord(ch) - 97] = 2 if s >> i & 1 else 1
ans.append(f)
return ans