How to find the longest common prefix of two strings in Scala?
I probably can code an "imperative" solution (with an index i
running over the strings while s(i) == t(i)
) but I am looking for a "functional-style" solution (without updating the index variable explicitly, for instance).
scala> "helloworld".zip("hellohell").takeWhile(Function.tupled(_ == _)).map(_._1).mkString
res130: String = hello
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With