Beginner question: algebraic properties of string rotations I was recently considering the problem of determining if one string is a rotation of another. It's a fairly common software engineering problem, but since I recently finished taking abstract algebra I was inspired to ask this from a more algebraic perspective.
First, if we represent the alphabet as a = 0, b = 1, etc., then it becomes equivalent to $\mathbb Z_{26}$ (a = 0, b = 1, etc.).
While $\mathbb Z_{26}$ does, in fact, form a multiplicative group, I'm not sure that that fact actually helps me all that much, so my first thought was to represent this problem using a permutation group instead. For example, if we had $abc$ and $cba$ that would be $a \mapsto c$, $b \mapsto b$, and $c \mapsto a$. Similarly, if we had the strings $abc$ and $bca$, then $a \mapsto b$, $b \mapsto c$, and $c \mapsto a$.
Another example: $abcdef$ and $defabc$. In this case, we can represent the "shift length" as $-3$ ($d = 3$, $a = 0$, $a - d = 0 - 3 = -3$), $-3$ ($b = 1$, $e = 4$, $1 - 4 = -3$), $-3$, $3$, $3$, $3$. (Annoyingly enough, though, $-3 \notin \mathbb Z_{26}$, which would obviously violate the original constraint of my problem).
A few things I notice about this (and I've noticed the same thing about the other strings that I've tried this for):


*

*It would appear that, if $length > 1$, there are two distinct "groups" of shift length (and I'm using "groups" in a looser sense, not in the sense of a mathematical group) - in this case $-3, -3, -3$ and $3, 3, 3$. Each of these will have either $\left \lfloor{length / 2} \right \rfloor$ or $\left \lceil{length / 2} \right \rceil$ numbers.

*The other thing I notice: $|-3| = 3$. I think that, for any two shifts of a single rotation, $x, y$, $|x|, |y| \in \mathbb Z_{26} \wedge |x| = |y|$. (I'm not quite as sure about this one though, $abc \mapsto cab$ is a possible counterexample - I haven't quite figured out how to fix it for this case though).


My idea is that a) these properties will always hold for any two arbitrary string rotations and b) these properties constitute a "reliable" test for string rotation as I describe it above (i.e. if these properties hold then it'll always be the case that the strings are rotations of each other).
I have code that actually implements a function based on these properties that seems to work for the strings I've tested it for, but I'm not sure exactly how relevant that is here (I can post it if anyone's curious; otherwise, I'll probably end up posting it on Code Review SE if people agree that my alleged properties actually hold since it's on topic there).
With that rather lengthy introduction complete, my first question is: are these actually true? Also, I'm a little embarrassed to ask this, but I'm having a hard time formalizing these ideas into an actual theorem (I'm admittedly slightly clumsy with permutation groups still); can someone help me formalize this more precisely than I have above?
 A: You might be interested in learning about cyclic codes in algebraic coding theory.
A cyclic code is one in which cyclic shifts of codewords are again codewords. Cyclic codes of length $n$ over a field $F$ are in one to one correspondence with the ideals of the quotient ring $F[x]/(x^n-1)$. This makes it easy to quickly find generator and parity check polynomials which totally describe the code.
If you are interested in one particular word and its shifts, then you'd be looking at a principal ideal generated by the codeword you have in mind. If your alphabet has a nonprime number of characters, it might be beneficial to pad it up to a power of a prime number so that you can use a field to encode the characters. You can work with $\mathbb Z_{26}$ if you want but I think the results will be slightly less sharp.
Alternatively, maybe you'd rather model this as a group action. If you allow the group of cyclic permutations to act on $n$-tuples (for some fixed $n$) then the words which are permutations of each other share the same orbit of the group action.
If you just have two words of the same length and want to determine if one is a cyclic permutation of the other, it seems like the algorithm would be pretty straightforward, though, without any bells and whistles. If $a$ and $b$ are the strings, you just scan through $b$ looking for the first character of $a$, chop the string there and concatenate the two pieces in reverse order, then compare the strings for equality.
A: I think you're confusing the alphabet (the possible values that can occupy a position in the string) with the positions themselves. This isn't too problematic in the examples you give, but what if you want to know if 'baby' is a rotation of 'byba'?
So I would suggest discarding the $\mathbb{Z}_{26}$ approach (unless you're thinking of strings of length 26), and instead to think of your strings (of length $n$) as the set of all functions 
$$S = \{f \mid f: \mathbb{Z}_n \rightarrow A\}$$
where $A$ is some set of characters (e.g., the latin alphabet; or ('0, '1'), or whatever is appropriate).
Then a single slot rotation $r : S \rightarrow S$ is defined by 
$$r(f(i)) = f(i+1 \mod n)$$
We can think about the compositions $r(r(f))$, $r(r(r(f)))$, etc., which I will write as $r^2$, $r^3$, and so on. It should be apparent that
$$r^k(f(i)) = f(i + k \mod n)$$
and in fact $r$ with the operation 'composition' is isomorphic to the (additive) group $Z_n$.
In the example you gave, e.g. $abcdef \rightarrow defabc$, it's true that $3 = -3 \mod 6$; leading you to think "aha! and $|-3| = 3$" but that's really an accident of your rotation of length 6. In the rotation $abcdef \rightarrow fabcde$, $|-5| \neq 1$; but on the other hand it is true that $-5 = 1 \mod 6$.
Then, if you are so lucky to start with a special string like $abcdefg$, where you could also say it is the string
$$\forall i \in \mathbb{Z}_n: f(i) = i$$
Then a string $g$ is rotation of $f$ if and only if there is some $k \in \mathbb{Z}_n$ s.t. $g = r^k(f)$; which is to say that $\forall i \in \mathbb{Z}_n$:
$$g(i) = r^k(f(i))$$
$$= f(i + k \mod n)$$
And in your special $abcdef$-style cases, we can go further and say
$$g(i) = i + k \mod n$$
$$= f(i) + k \mod n$$
so
$$g(i) - f(i) = k \mod n$$
which is what I think you were reaching for when observing $|-3| = 3$.
