You are given a sequence of distinct elements $P$ and a positive integer $R$. Consider the sequence obtained by repeating $P$ $R$ times and concatenating together. For example, if $P = \langle A, B\rangle$ and $R = 3$, then the resulting sequence $P^R$ is $\langle A, B, A, B, A, B \rangle$.
We would like to transform this sequence into another sequence such that identical elements are now adjacent to each other (more formally, for a sequence $S$, we would like to find a permutation $S'$ of $S$ such that for any two $S'_i$ and $S'_j$ such that $S'_i = S'_j$, for any integer $i \le k \le j$ we must have $S'_i = S'_k = S'_j$). The only operation we can perform on this sequence is swapping two elements.
For example, with $P = \langle A, B \rangle$, let $S = P^3 = \langle A, B, A, B, A, B \rangle$. Consider $S' = \langle A, A, A, B, B, B \rangle$. We can transform $S$ to $S'$ by swapping $S_2$ and $S_5$, thus, we can obtain $S'$ from $S$ by using a single swap. We can also consider $S' = \langle B, B, B, A, A, A \rangle$, in which case we need two swaps.
Is there an algorithm to do this? How should this be done if we want the minimum number of swaps?
Disclaimer: This solution is provably optimal with the assumption that the function we want to minimize is the number of elements that have to be swapped. I think this should also be optimal for the other case, but I do not give such proof.
We denote by "displaced element" the elements of $S$ that is different than the element of $S'$ in the same location (that is, the element that need to be swapped). If $R$ divides $|P|$, then we can use any possible $S'$ and we will have the same number of displaced elements. Thus, we consider the case where $R \not = P$.
We will first consider the case where $|P|$ and $R$ are coprime to each other. $S'$ consists of $|P|$ group of $R$ consecutive characters, one for each element of $P$. Let the element of the $i$-th group of consececutive characters in $S'$ be noted by $G_{S'}(i)$ (for example, if $S' = \langle A, A, B, B \rangle$, then $G_{S'}(1) = A$ and $G_{S'}(2) = B$).
Now, consider $S'$ obtained by setting $G_{S'}(i)$ to the $(((i-1)R \mod |P|) + 1)$-th character in $P$. Since $|P|$ and $R$ are coprime to each other, $1 \le R \mod |P| < |P|$, and thus all the elements of $G_{S'}$ are unique. In particular, each character in $P$ appears exactly once. Thus, $S'$ is a valid transformed sequence.
Now we claim that this $S'$ consist of a minimum number of displaced elements. We show this by showing that for each group in $S'$, there exists exactly $\lceil R / |P| \rceil$ elements that are not displaced (note that this is the maximum possible such elements for each group, thus this is optimal). The index of the first character of the $i$-th group is $(i-1) R + 1$. However, the $(i \cdot R + 1)$-th character of $P^R$ is exactly equal to $G_{S'}(i)$ and thus the claim holds.
Now suppose $R$ and $|P|$ are not coprime to each other. Let $x = \gcd(R, |P|)$ and $y = \text{lcm}(R, |P|)$. We now set $G_{S'}(i)$ to the $(x((i-1)(R/x) \mod (|P|/x)) + \lfloor (i-1) / y \rfloor + 1)$-th character of $P$. This is also optimal and have the same guarantees as the case where $R$ and $|P|$ are coprime.