Find the longest common prefix from a list of strings. For example, from a list ['tess', 'tester', 'testing'], the longest common prefix is 'tes' because that is the longest substring that is shared between them.
Test cases in Scala
Algorithm in Scala
10 lines of Scala (version 2.13), showing how concise Scala can be!
First, we should find the shortest string in the list because that is the maximum possible longest prefix. Once we find that, we iterate through each of the indices to find the last index that has the same character for all the strings. (this is © from www.scala-algorithms.com)
Scala concepts & Hints
Drop, Take, dropRight, takeRight
Scala's `drop` and `take` methods typically remove or select `n` items from a collection.
The for-comprehension is highly important syntatic enhancement in functional programming languages.
The 'Option' type is used to describe a computation that either has a result or does not. In Scala, you can 'chain' Option processing, combine with lists and other data structures. For example, you can also turn a pattern-match into a function that return an Option, and vice-versa!
(1 to n)syntax produces a "Range" which is a representation of a sequence of numbers.
.viewsyntax creates a structure that mirrors another structure, until "forced" by an eager operation like .toList, .foreach, .forall, .count.