Skip to content

Instantly share code, notes, and snippets.

@andiogenes
Created November 20, 2019 14:09
Show Gist options
  • Save andiogenes/0be33a23657c2799e510ad6730e3b598 to your computer and use it in GitHub Desktop.
Save andiogenes/0be33a23657c2799e510ad6730e3b598 to your computer and use it in GitHub Desktop.
Slow but useful implementations of suffix array and LCP
fun naiveLCP(string: String): List<Int> {
val suffixes = makeSuffixArray(string)
val lcp = (suffixes.indices).map {
if (it - 1 < 0) {
0
} else {
string.substring(suffixes[it - 1]).commonPrefixWith(string.substring(suffixes[it])).length
}
}
return lcp
}
fun naiveSuffixArray(string: String): List<Int> {
return string.indices.sortedWith(Comparator { o1, o2 ->
string.substring(o1).compareTo(string.substring(o2))
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment