Методы, которые создают представление эквивалентности вращения строк.
def normalizedRepresentation(str: String) = {
str.map(char => (char - str.charAt(0) + 'a').asInstanceOf[Char])
}
def rotationalEquivalenceRepresentation(str: String) = {
val normalizedStr = normalizedRepresentation(str)
normalizedStr.map(char => if (char < 'a') (char + 26).asInstanceOf[Char] else char.asInstanceOf[Char])
}
затем
scala> val input = List("ac","bd","ce", "aaa","xyz","bbb","abc","kt","zb")
input: List[String] = List(ac, bd, ce, aaa, xyz, bbb, abc, kt, zb)
scala> input.map(str => normalizedRepresentation(str))
res0: List[String] = List(ac, ac, ac, aaa, abc, aaa, abc, aj, aI)
scala> input.map(str => rotationalEquivalenceRepresentation(str))
res1: List[String] = List(ac, ac, ac, aaa, abc, aaa, abc, aj, ac)
scala>
scala> input.groupBy(str => rotationalEquivalenceRepresentation(str))
res2: scala.collection.immutable.Map[String,List[String]] = Map(abc -> List(xyz, abc), aaa -> List(aaa, bbb), ac -> List(ac, bd, ce, zb), aj -> List(kt))
scala> input.groupBy(str => rotationalEquivalenceRepresentation(str)).values.toList
res3: List[List[String]] = List(List(xyz, abc), List(aaa, bbb), List(ac, bd, ce, zb), List(kt))