I have group elements, that I'd like to condense down according to group rules. Take the modular group. This has generators $S,T$ that satisfy the groups relations $S^2 = I, (ST)^3 = I$.
So given a group element as a list of generators (for instance, $S T^2 S$ I'd write as {S,T,T,S} in Mathematica), I'd like to collapse the expression down according to group rules.
For instance, in our example, we'd have
Collapse[{S,T,S,S,T}] = {S,T,T}
I've tried using ReplaceAll, but can't get it to match substrings. StringReplace seems to have the correct functionality, but only works on actual strings.
How would one go about doing this?