Search code examples
stringscalasuffix-array

Suffix array beginning using scala


Today I am trying to create suffix arrays using scala. I was able to do it with massive lines of code but then I heard that it can be created by using only few lines by using zipping and sorting.

The problem I have at the moment is with the beginning. I tried using binary search and zipWithIndex to create the following "tree" but so far I haven't been able to create anything. I don't even know if it is possible by only using a line but I bet it is lol.

What I want to do is to get from a word "cheesecake" is a Seq:

 Seq((cheesecake, 0),
     (heesecake, 1),
     (eesecake, 2),
     (esecake, 3),
     (secake, 4),
     (ecake, 5),
     (cake, 6),
     (ake, 7),
     (ke, 8),
     (e, 9))

Could someone nudge me to the correct path ?


Solution

  • To generate all the possible postfixes of a String (or any other scala.collection.TraversableLike) you can simply use the tails method:

    scala> "cheesecake".tails.toList
    res25: List[String] = List(cheesecake, heesecake, eesecake, esecake, secake, ecake, cake, ake, ke, e, "")
    

    If you need the indexes, then you can use GenIterable.zipWithIndex:

    scala> "cheesecake".tails.toList.zipWithIndex
    res0: List[(String, Int)] = List((cheesecake,0), (heesecake,1), (eesecake,2), (esecake,3), (secake,4), (ecake,5), (cake,6), (ake,7), (ke,8), (e,9), ("",10))