Created
September 18, 2013 23:02
-
-
Save 89465127/6616955 to your computer and use it in GitHub Desktop.
Longest Palandrome in Scala
This file contains hidden or 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
object Palandrome { | |
def main(args: Array[String]) { | |
val s = "meow woow ooooooo doggies aaadddfffdddaaa wow" | |
println("finding the longest palandrome of: " + s) | |
println(longestPal(s)) | |
} | |
def longestPal(s: String) = { | |
longestPalN(s, s.length) | |
} | |
def longestPalN(s: String, n: Int): String = { | |
s.sliding(n).find(s => s == s.reverse) match { case Some(s) => s; case None => longestPalN(s, n-1)} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment