(s)
Find the rotation of s that is smallest in lexicographic order.
    Duval 1983 describes how to modify his algorithm to do so but I think
    it's cleaner and more general to work from the ChenFoxLyndon output.
Find the rotation of s that is smallest in lexicographic order.
    Duval 1983 describes how to modify his algorithm to do so but I think
    it's cleaner and more general to work from the ChenFoxLyndon output.
Find the rotation of s that is smallest in lexicographic order . Duval 1983 describes how to modify his algorithm to do so but I think it s cleaner and more general to work from the ChenFoxLyndon output .
def SmallestRotation(s):
    """Find the rotation of s that is smallest in lexicographic order.
    Duval 1983 describes how to modify his algorithm to do so but I think
    it's cleaner and more general to work from the ChenFoxLyndon output."""
    prev,rep = None,0
    for w in ChenFoxLyndon(s+s):
        if w == prev:
            rep += 1
        else:
            prev,rep = w,1
        if len(w)*rep == len(s):
            return w*rep
    raise Exception("Reached end of factorization with no shortest rotation")
def SmallestRotation ( s ) : prev , rep = None , 0 for w in ChenFoxLyndon ( s + s ) : if w == prev : rep += 1 else : prev , rep = w , 1 if len ( w ) * rep == len ( s ) : return w * rep raise Exception ( "Reached end of factorization with no shortest rotation" )