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 6117847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:22:03+00:00 2026-05-23T15:22:03+00:00

please take a quick look at this function that I have found on the

  • 0

please take a quick look at this function that I have found on the web.

function longestCommonSubstring(string1, string2){
        // init max value
        var longestCommonSubstring = 0;
        // init 2D array with 0
        var table = Array(string1.length);
        for(a = 0; a <= string1.length; a++){
                table[a] = Array(string2.length);
                for(b = 0; b <= string2.length; b++){
                        table[a][b] = 0;
                }
        }
        // fill table
        for(var i = 0; i < string1.length; i++){
                for(var j = 0; j < string2.length; j++){
                        if(string1[i]==string2[j]){
                                if(table[i][j] == 0){
                                        table[i+1][j+1] = 1;
                                } else {
                                        table[i+1][j+1] = table[i][j] + 1;
                                }
                                if(table[i+1][j+1] > longestCommonSubstring){
                                        longestCommonSubstring = table[i+1][j+1];
                                }
                        } else {
                                table[i+1][j+1] = 0;
                        }
                }
        }
        return longestCommonSubstring;
}

It returns the length of the longest common substring as an int. Now to my question, is it possible to modify this function, so that it returns the actual string instead of just returning the length of the substring, I’m quite new at programming and thought that just modifying this secetion would help if(string1[i]==string2[j]){ push(string1[i]}, but it isn’t that easy, because I don’t want every single character that is the same in those 2 strings to be added in that array, only those that are exactly the same.
Thanks in advance =)

  • 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-23T15:22:04+00:00Added an answer on May 23, 2026 at 3:22 pm

    Well for minimal changes to the existing function you could declare a new variable:

    var theCommonString = '';
    

    Then in the middle of the function add a line after this existing one:

    longestCommonSubstring = table[i+1][j+1];
    

    that says something like:

    theCommonString = string1.substr(i + 1 - longestCommonSubstring,
                                     longestCommonSubstring);
    

    (That i + 1 index may be a little off, I haven’t bothered working it out carefully.)

    Then at the end just return your new variable instead of the existing one.

    Note that if there is more than one common sub string of the same length this will return the last one.

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

Sidebar

Related Questions

Please suppose I have a function that accepts a pointer as a parameter. This
Please take a look at this site as a reference: http://www.philsalesses.com/place-pulse/ On the left
Please take a look at this navigation: http://beta.viewcms.info/mint/ And you can see the hover
If you have any knowledge of XML layouts in Android, please take a quick
Please take a look at this: https://i.stack.imgur.com/9UOhU.jpg The problem: The grid is drawn perfectly
Please take a look at the html listed below and let me know why
Could somebody please take some time to show me a quick example on how
please take a look at the following website: http://www.solidcamxpress.co.uk/ If you look at it
please take a look at the blue menu bar at the top of http://www.animefushigi.com
Please take a look at the following JavaScript. I've taken stuff out of it,

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.