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

  • Home
  • SEARCH
  • 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 314089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:10:05+00:00 2026-05-12T08:10:05+00:00

We need to combine 3 columns in a database by concatenation. However, the 3

  • 0

We need to combine 3 columns in a database by concatenation. However, the 3 columns may contain overlapping parts and the parts should not be duplicated. For example,

  "a" + "b" + "c" => "abc"
  "abcde" + "defgh" + "ghlmn" => "abcdefghlmn"
  "abcdede" + "dedefgh" + "" => "abcdedefgh"
  "abcde" + "d" + "ghlmn" => "abcdedghlmn"
  "abcdef" + "" + "defghl" => "abcdefghl"

Our current algorithm is pretty slow because it uses brute-force to identify the overlapping part between 2 strings. Does any one know an efficient algorithm to do this?

Say we have 2 strings A and B. The algorithm needs to find the longest common substring S so that A ends with S and B starts with S.

Our current brute-force implementation in Java is attached for reference,

public static String concat(String s1, String s2) {
    if (s1 == null)
        return s2;
    if (s2 == null)
        return s1;
    int len = Math.min(s1.length(), s2.length());

    // Find the index for the end of overlapping part
    int index = -1;
    for (int i = len; i > 0; i--) {
        String substring = s2.substring(0, i);
        if (s1.endsWith(substring)) {
            index = i;
            break;
        }
    }
    StringBuilder sb = new StringBuilder(s1);
    if (index < 0) 
        sb.append(s2);
    else if (index <= s2.length())
        sb.append(s2.substring(index));
    return sb.toString();
}
  • 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-12T08:10:05+00:00Added an answer on May 12, 2026 at 8:10 am

    Most of the other answers have focused on constant-factor optimizations, but it’s also possible to do asymptotically better. Look at your algorithm: it’s O(N^2). This seems like a problem that can be solved much faster than that!

    Consider Knuth Morris Pratt. It keeps track of the maximum amount of substring we have matched so far throughout. That means it knows how much of S1 has been matched at the end of S2, and that’s the value we’re looking for! Just modify the algorithm to continue instead of returning when it matches the substring early on, and have it return the amount matched instead of 0 at the end.

    That gives you an O(n) algorithm. Nice!

        int OverlappedStringLength(string s1, string s2) {
            //Trim s1 so it isn't longer than s2
            if (s1.Length > s2.Length) s1 = s1.Substring(s1.Length - s2.Length);
    
            int[] T = ComputeBackTrackTable(s2); //O(n)
    
            int m = 0;
            int i = 0;
            while (m + i < s1.Length) {
                if (s2[i] == s1[m + i]) {
                    i += 1;
                    //<-- removed the return case here, because |s1| <= |s2|
                } else {
                    m += i - T[i];
                    if (i > 0) i = T[i];
                }
            }
    
            return i; //<-- changed the return here to return characters matched
        }
    
        int[] ComputeBackTrackTable(string s) {
            var T = new int[s.Length];
            int cnd = 0;
            T[0] = -1;
            T[1] = 0;
            int pos = 2;
            while (pos < s.Length) {
                if (s[pos - 1] == s[cnd]) {
                    T[pos] = cnd + 1;
                    pos += 1;
                    cnd += 1;
                } else if (cnd > 0) {
                    cnd = T[cnd];
                } else {
                    T[pos] = 0;
                    pos += 1;
                }
            }
    
            return T;
        }
    

    OverlappedStringLength(“abcdef”, “defghl”) returns 3

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

Sidebar

Ask A Question

Stats

  • Questions 148k
  • Answers 148k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The problem: Java can't find the JDBC Driver Class. Solution:… May 12, 2026 at 9:20 am
  • Editorial Team
    Editorial Team added an answer It sounds like one or more of the OLEDB components… May 12, 2026 at 9:20 am
  • Editorial Team
    Editorial Team added an answer private static void ConfigureStoryboard(DoubleAnimation animation, Storyboard storyboard, EasingFunction function) {… May 12, 2026 at 9:20 am

Related Questions

We're having a small issue and could use some help - we have the
I need to calculate the DateDiff (hours) between two dates, but only during business-hours
We have a large high-performance software system which consists of multiple interacting Java processes
We are using version 3.0.20229.0 of the asp.net ajaxControlTookKit (before .net 3.5 sp1). I
We are in the process of porting a legacy system to .NET, both to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.