Created
November 20, 2019 14:09
-
-
Save andiogenes/0be33a23657c2799e510ad6730e3b598 to your computer and use it in GitHub Desktop.
Slow but useful implementations of suffix array and LCP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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