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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:23:06+00:00 2026-06-13T19:23:06+00:00

I am trying to find the largest substrings which are in both strings (minimum

  • 0

I am trying to find the largest substrings which are in both strings (minimum length of 3). So if I have:

String test1 = "testthatthisworks";
String test2 = "testthisthat";

I need the answer to be:

String[] Answer = ["test", "that", "this"];

My one problem is this needs to be as fast as possible. My current solution is to with a substring of length 3 from the smallest string, and then to see if this exists in the bigger string, if it does increment the size of the substring, if not move the substring along 1 point. The problem is that this is very slow as the strings grow in length. Does anyone have a solution to this problem?

Thanks

  • 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-06-13T19:23:07+00:00Added an answer on June 13, 2026 at 7:23 pm

    This is a modification of the LCS algorithm, it will return all maximum length matches
    of maximum size:

    public static Collection<String> longestCommonSubstrings(String S1, String S2){
      return longestCommonSubstrings(S1, S2, 0);
    }
    
    public static Collection<String> longestCommonSubstrings(String S1, String S2, int minimumLength){
    
    Collection<Integer> indexes = new ArrayList<Integer>();
    int Max = minimumLength;
    
    for (int i = 0; i < S1.length(); i++){
      for (int j = 0; j < S2.length(); j++){
        int x = 0;
        int y = Math.min(S1.length()-i,S2.length()-j);
        while (x < y && (S1.charAt(i + x) == S2.charAt(j + x) )){
          x++;
        }
        if (x > Max){
          Max = x;
          indexes = new ArrayList<Integer>();
          indexes.add(i);
        }else if (x == Max){
          indexes.add(i);
        }
      }
    }
    Collection<String> results = new HashSet<String>();
    for (Integer i : indexes){
      results.add(S1.substring(i, (i + Max)));
    }
    return results;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to find: Show the top-20 PIs who have the largest total amount of
I have a collection List<CustomClass> which I am trying to find a way to
I'm trying to find the largest prime factor of the number x, Python gives
I'm currently trying to find the largest prime number contained within another large number.
I'm trying to find the largest prime factor of the number 600851475143. I dimmed
I am trying to find the max number in an array. I have created
I am trying to write a program to find the largest prime factor of
Have been pulling out my hair trying to find out why my sessions are
I'm trying find a way to have PHP to indicate to the browser that
Trying to find a work around for users who have IE 7. Basically in

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.