Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the longest common prefix of two strings in Scala?

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).

like image 954
Michael Avatar asked Nov 12 '11 12:11

Michael


1 Answers

scala> "helloworld".zip("hellohell").takeWhile(Function.tupled(_ == _)).map(_._1).mkString
res130: String = hello
like image 73
missingfaktor Avatar answered Sep 28 '22 16:09

missingfaktor