Skip to content

Instantly share code, notes, and snippets.

@rirakkumya
Created March 18, 2012 15:00
Show Gist options
  • Save rirakkumya/2074828 to your computer and use it in GitHub Desktop.
Save rirakkumya/2074828 to your computer and use it in GitHub Desktop.
最長重複文字列問題
def maxDupStr(str:String) = {
def tails:String => List[String] = {
case "" => Nil
case x => x :: tails(x.tail)
}
val sorted = tails(str) sorted
val pair = sorted zip sorted.tail
val maxPair = {
pair map { case (s1,s2) => (s1 zip s2 filter {c => c._1 == c._2} size, s1)} max
}
maxPair._2 take maxPair._1
}
assert(maxDupStr("abcab") == "ab")
assert(maxDupStr("abcdefgcdef") == "cdef")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment