Skip to content

Commit

Permalink
fixed bug breaking longestCommonSubstring if first arg shorter than s…
Browse files Browse the repository at this point in the history
…econd, fixes vkostyukov#27
  • Loading branch information
longshorej committed Nov 21, 2016
1 parent 5898ecd commit 59fc41d
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/primitive/Strings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ object Strings {
* Time - O(mn)
* Space - O(mn)
*/
def longestCommonSubstring(a: String, b: String) : String = {
def longestCommonSubstring(one: String, two: String) : String = {
val (a, b) =
if (one.length <= two.length) (one, two)
else (two, one)

def loop(m: Map[(Int, Int), Int], bestIndices: List[Int], i: Int, j: Int) : String = {
if (i > a.length) {
b.substring(bestIndices(1) - m((bestIndices(0),bestIndices(1))), bestIndices(1))
Expand Down

0 comments on commit 59fc41d

Please sign in to comment.