I read this question which describes the following expression about the problem:
You are given two meanings: Nand K. A moon dog is interested in lines that satisfy the following conditions:
- The string has exactly
Ncharacters, each of which is either "A" or "B". - A string
shas exactly Kpairs (i, j)( 0 <= i < j <= N-1) such that s[i] = 'A'and s[j] = 'B'.
If there is a string that satisfies the conditions, find and return any such string. Otherwise, return an empty string
It seems to me that this problem is equivalent:
Determine whether 2-partitions exist 0...N-1for which the Cartesian product contains exactly Ktuples (i, j)withi < j
If tuple elements represent string index assignments to Aand B.
This gives a naive (but correct) implementation:
- Define all 2-partitions of a set
0...N-1 - For each such partition, a Cartesian product of subsets is produced.
- For each Cartesian product, count the number of tuples
(i, j)for whichi < j - Select any 2-section for which this counter
K
Here is the implementation in JS:
const test = ([l, r]) =>
cart(l, r).reduce((p, [li, ri]) => p + (li < ri ? 1 : 0), 0) === k
const indices = _.range(0, n)
const results = partitions(indices).filter(test)
You can check the results in the context of the original problem here . Some examples of outputs for n = 13, are k = 29:
"aababbbbbbbbb", "babaaabbbbbbb", "baabababbbbbb", "abbaababbbbbb", ...
- : S(n, k) k = 2:

, n=13 4095, .
, , ( ), , . , K , i < j.
, - , . . 2- , ?