A. Equal Distribution
The answer is $$$YES$$$ if and only if the number of red balls and the number of blue balls are both divisible by $$$2$$$.
B. Smallest String
The solution will not exist if $$$n < k$$$ because each letter must be used at least once. Further, if $$$k = 1$$$ and $$$n > 1$$$, the solution will not exist because each pair of adjacent characters must be different.
In all other cases, we can greedily construct the solution from left to right by maintaining number of characters not yet used, $$$r$$$; and the previous character, $$$prev$$$.To decide for the $$$i$$$-th position we do the following:
- If $$$r < n+1-i$$$, we can place the lexicographically smallest letter which is not equal to $$$prev$$$. (This will always be either "a" or "b".)
- Otherwise, we can place the smallest unused letter and decrease $$$r$$$ by $$$1$$$.
Time Complexity: $$$O(|A|*n)$$$ where $$$|A| = 26$$$ denotes the alphabet size.
It can also be done in $$$O(n)$$$ independent of $$$|A|$$$ but that is not required in this problem.