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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:02:04+00:00 2026-05-16T10:02:04+00:00

This question is slightly different from the kind of finding longest sequence or substring

  • 0

This question is slightly different from the kind of finding longest sequence or substring from two strings.

Given two string of the same size N, find the longest substrings from each string such that the substrings contains the same bag of chars.

The two substrings may not necessarily have the same sequence. But they must have the same bag of chars.

For example,

a = ABCDDEGF
b = FPCDBDAX

The longest matching bag of chars are ABCDD (ABCDD from a, CDBDA from b)

How to solve this problem?


UPDATE

The goal is to find substrings from each input string, such that they have the same bag of chars. By saying “substring”, they must be consecutive chars.


Update: Initially I thought of a dynamic programming approach. It works as below.

To compare two bags of chars of the same length K, it would take O(K) time to achieve so. Convert each string into a shorten form:

ABCDDEGF -> A1B1C1D2E1G1F1
FPCDBDAX -> A1B1C1D2F1P1X1

The shorten form is sorted alphabets followed by number of frequencies in the string.
Construct, sort, and compare the shorten forms would take O(K) time in total. (Implementation can be achieved by using an array of chars though)

Two bags of chars are equal iif their shorten forms have the same chars and respective frequencies.

In addition, it takes O(logK) time to find the difference chars between the two string.

Now, for two input strings:

  1. If their shorten forms are identical, then this is the longest common bag of chars.
  2. Find chars in string1 such that they do not appear in string2. Tokenize string1 into several substrings based on those chars.
  3. Find chars in string2 such that they do not appear in string1. Tokenize string2 into several substrings based on those chars.
  4. Now, we have two list of strings. Compare each pair (which in turn is the same problem with a smaller size of input) and find the longest common bag of chars.

The worst case would be O(N3), and best case would be O(N). Any better idea?

  • 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-16T10:02:05+00:00Added an answer on May 16, 2026 at 10:02 am

    Create a set of the characters present in a, and another of the characters present in b. Walk through each string and strike (e.g., overwrite with some otherwise impossible value) all the characters not in the set from the other string. Find the longest string remaining in each (i.e., longest string of only “unstruck” characters).

    Edit: Here’s a solution that works roughly as noted above, but in a rather language-specific fashion (using C++ locales/facets):

    #include <string>
    #include <vector>
    #include <iostream>
    #include <locale>
    #include <sstream>
    #include <memory>
    
    struct filter : std::ctype<char> {
        filter(std::string const &a) : std::ctype<char>(table, false) {
            std::fill_n(table, std::ctype<char>::table_size, std::ctype_base::space);
    
            for (size_t i=0; i<a.size(); i++) 
                table[(unsigned char)a[i]] = std::ctype_base::upper;
        }
    private:
        std::ctype_base::mask table[std::ctype<char>::table_size];
    };
    
    std::string get_longest(std::string const &input, std::string const &f) { 
        std::istringstream in(input);
        filter *filt = new filter(f);
    
        in.imbue(std::locale(std::locale(), filt));
    
        std::string temp, longest;
    
        while (in >> temp)
            if (temp.size() > longest.size())
                longest = temp;
        delete filt;
        return longest;
    }
    
    int main() { 
        std::string a = "ABCDDEGF",  b = "FPCDBDAX";
        std::cout << "A longest: " << get_longest(a, b) << "\n";
        std::cout << "B longest: " << get_longest(b, a) << "\n";
        return 0;
    }
    

    Edit2: I believe this implementation is O(N) in all cases (one traversal of each string). That’s based on std::ctype<char> using a table for lookups, which is O(1). With a hash table, lookups would also have O(1) expected complexity, but O(N) worst case, so overall complexity would be O(N) expected, but O(N2) worst case. With a set based on a balanced tree, you’d get O(N lg N) overall.

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

Sidebar

Related Questions

This is a slightly different question to this one ([ Accessing a method from
slightly different question about variance this time. I take it from experimentation that C#
Similar question here but this is slightly different... I have two tables that I
I know this question has been done but I have a slightly different twist
I believe this question is slightly different than similar ones asked on here before
I'm going to re-ask this question in a slightly different way. Say I have
This question has been asked before but I am facing a slightly different problem.
My question is similar to this question, but as always is slightly different. I
This question comes close to what I need, but my scenario is slightly different.
There is a similarly worded question, but I think this is slightly different. Basically,

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.