January 31, 2012
This is easy if you know the trick, and harder if you don’t. The two strings are rotations of each other if they are the same length and if the one of the strings is contained in the concatenation of the other string with itself:
(define (string-rotated? s1 s2)
(and (= (string-length s1) (string-length s2))
(string-find s2 (string-append s1 s1))))
This function goes beyond the requirement: if one string is a rotation of the other, it tells how many characters must be moved from the beginning to the end of the first string in order to make it the same as the second string.
Pages: 1 2