TITLE: Number of possible configurations by shifting the '2's in $12121212$ to the right.
QUESTION [7 upvotes]: I came up with this question while solving another combinatorics problem.

Let's say there is a number $12121212$. Define an operation as swapping any two adjacent digits if the left digit is $2$. (For example, swapping the $2$$nd$ and $3$$rd$ digit to give $11221212$ as a result, but swapping the $3$$rd$ and $4$$th$ digit is not allowed.) There is no limit on how many operations you can do on the number(no operation is also possible). How many possible numbers can be formed?

Questions

Is there a name to this kind of problems?
How can it be solved?
Extra: What if the original is not $12121212$ but some other numbers(like for example$121212121111111111$? Will this make the question very complicated?

My attempt
I am not sure how to approach this question. My observation is that the final configuration $11112222$ remains unchanged after any operations. So it seems that the first '$2$' originally at $2$$nd$ position moves to the $5$$th$  position, the first '$2$' orginally at $4$$th$ position moves to the $6$$th$ position and so on.
However, some of the cases are invalid but at least I know that number of possible configurations is less than $4\cdot3\cdot2\cdot1 = 24$. So a possible way will be to enumerate all possible configurations, but it is a pain because I cannot find a way to do it in an organised manner. Therefore, I am curious if there is a way to do it more efficiently and smartly.

REPLY [2 votes]: Think of these strings as describing mountain paths from $\langle 0,0\rangle$ to $\langle 2n,0\rangle$, where $n$ is the number of $1$s (or $2$s): each $1$ corresponds to an up-step from $\langle x,y\rangle$ to $\langle x+1,y+1\rangle$, and each $2$ to a down-step from $\langle x,y\rangle$ to $\langle x+1,y-1\rangle$. Initially we have a path that looks like this:
             /\/\/\/\.../\

Each legal move consists in interchanging a down-step with the step to its immediate right. If that step is also a down-step, the path doesn’t change. Otherwise a sequence \/ is converted to a sequence /\. We still have $n$ up-steps and $n$ down-steps, so the path still ends at $\langle 2n,0\rangle$, and an easy induction shows that no path obtainable in this way drops below the $x$-axis.
It takes a bit more work to show that every mountain path from $\langle 0,0\rangle$ to $\langle 2n,0\rangle$ that never drops below the $x$-axis is obtainable in this way, but once we have that, we’re done: it’s well-known that the number of such paths is $C_n$, the $n$-th Catalan number.
The idea is simple enough. Take any such mountain path $P$. Reading from left to right, find the first peak at a height greater than $1$. (If there isn’t one, we’re done: it’s our initial path.) That peak consists of an up-step followed by a down-step; interchange those two steps. This interchange is simply the inverse of the legal move in the original procedure. Repeat this process until there are no more peaks of height greater than $1$. At that point you have the path
             /\/\/\/\.../\,

and $P$ can clearly be obtained from it by a sequence of legal moves.