如何在Scala中找到两个字符串的最长公共前缀? [英] How to find the longest common prefix of two strings in Scala?
本文介绍了如何在Scala中找到两个字符串的最长公共前缀?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Scala中找到两个字符串的最长公共前缀?
How to find the longest common prefix of two strings in Scala?
我可能可以编写一个命令式"解决方案(在s(i) == t(i)
上使用索引i
在字符串上运行),但是我正在寻找一个函数式"解决方案(例如,不显式更新索引变量) ).
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
这篇关于如何在Scala中找到两个字符串的最长公共前缀?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文