Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

largestCommonSubstring #27

Open
hhummel opened this issue Oct 20, 2016 · 0 comments
Open

largestCommonSubstring #27

hhummel opened this issue Oct 20, 2016 · 0 comments

Comments

@hhummel
Copy link

hhummel commented Oct 20, 2016

Strings.longestCommonSubstring gives incorrect results if the first string is shorter than the second one, because it only scans until the length of the first string:

object Strings {
...

def longestCommonSubstring(a: String, b: String) : String = {
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))
...

Doesn't work:
scala> val lcs = Strings.longestCommonSubstring("the price Is right", "right on!")
lcs: String = ri

Works correctly:
scala> val lcs = Strings.longestCommonSubstring("right on!", "the price is right")
lcs: String = right

longshorej added a commit to longshorej/scalacaster that referenced this issue Nov 21, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant