Generate One-Two Strings with Sum N
Problem statement
Given a positive integer n, return every string made only from the digits 1 and 2 whose digit values sum to n.
Return the strings in ascending lexicographic order, using the usual character order 1 < 2.
Function
generateOneTwoStrings(n: int) → String[]Examples
Example 1
n = 3return = ["111","12","21"]The valid digit sequences are 1 + 1 + 1, 1 + 2, and 2 + 1. They are already listed in lexicographic order.
Example 2
n = 4return = ["1111","112","121","211","22"]Each returned string sums to 4. Choosing 1 before 2 during construction produces the required order.
Example 3
n = 1return = ["1"]Only the one-character string 1 has digit sum 1.
Constraints
1 <= n <= 12.- Every returned string contains only
1and2. - Return every valid string exactly once in ascending lexicographic order.