TITLE: Concatenation of strings
QUESTION [5 upvotes]: We have two strings (i. e., finite tuples) $A$ and $B$.
We have to find if for some positive integers $n$ and $m$, the string $A$ concatenated $n$ times equals the string $B$ concatenated $m$ times or not.
I have made an interesting observation but am unable to prove it. It appears that this can happen if and only if $AB=BA$.

REPLY [4 votes]: Your question seems to be answered by Theorem 1.5.3 of Allouche and Shallit's Automatic Sequences: Theory, Applications, Generalizations (Cambridge University Press, 2003).

Let $x,y\in\Sigma^+$.  Then the following three conditions are equivalent:
  (1) $xy=yx$.
  (2) There exists integers $i,j>0$ such that $x^i=y^j$.
  (3) There exist $z\in\Sigma^+$ and integers $k,l>0$ such that $x=z^k$ and $y=z^l$.

The book has a proof.