Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1115605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T03:10:57+00:00 2026-05-17T03:10:57+00:00

I am trying to write a function which computes the length of the longest

  • 0

I am trying to write a function which computes the length of the longest common subsequence of the two input strings str1 and str2.

This is what I have right now,

(define LCS
  (lambda (str1 str2)
    (if (OR (equal? str1 "") (equal? str2 ""))
        0
        (if (equal? (string-contains str1 (string-ref str2 0)) #t) 
            (+ 1 (LCS (substring str1 1 (string-length str1)) 
                      (substring str2 1 (string-length str2))))
            (LCS (substring str1 1 (string-length str1)) 
                 (substring str2 1 (string-length str2)))))))

Where string-contains returns true if a string has a certain character in it. Right now it seems like it works, but I think there might be a bug.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T03:10:58+00:00Added an answer on May 17, 2026 at 3:10 am

    Your code is totally on the right track if you don’t mind a relatively slow algorithm; there’s a more sophisticated approach to the problem, with dynamic programming, if you need it to be faster.

    Right now, the bug in your code is that you’re moving down the length of both strings simultaneously with each recursive call. It would fail, for example (I think, I haven’t tried it) with the following two strings: (LCS "scheme" "emesch") Reason being is that the matching substrings don’t start at the same position in the first and second string.

    I suggest that you split up the recursion into two recursive calls: one where you remove a character off the front of only the first string, and one where you remove a character off the front of only the second. Then, you take the best result from either of those calls. In that fashion, you can be sure that you’ll find substrings no matter where they lie in the other string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What I am trying is write recursive function which returns the least common divisor
I am trying to write a function in C++ which takes two 64 bit
I am trying to write a function which returns a string created from two
i am trying to write a zoom function which looks something like this: centre
Hi I am trying to write a recursive function which calculates the length of
i am trying to write one function which return sql statement like function get_sql($name=0,$date_start=0,$date_end=0)
I'm trying to make it so I can write a function which defines a
I'm trying to write a function that takes multiple arguments, which can come either
I am currently learning Haskell. I am trying to write a function which given
Im trying to write a recursive function which returns the factorial of a number.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.