Solution (source code)

= Solution

The <normal form theorem for an amalgamated free product> says that, after choosing left coset representatives for $C$ in $A$ and $B$, each element of $A*_CB$ has a unique normal form consisting of an initial element of $C$ followed by an alternating word in nontrivial representatives from the two factors. In particular, every nonempty reduced alternating word whose syllables lie outside $C$ is nonidentity.

For the <free product> $A*B$, the amalgamated subgroup is trivial. Hence
$$
g=a_1b_1a_2\cdots a_kb_k
$$
is nontrivial whenever, after omitting a possibly empty initial or final syllable, every displayed $A$-syllable and $B$-syllable is nonidentity. It is then a nonempty reduced normal form.