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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:53:13+00:00 2026-06-12T12:53:13+00:00

I have two strings, hello an eo for instance, and I wish to find

  • 0

I have two strings, “hello” an “eo” for instance, and I wish to find duplicate characaters between the two strings, that is: ‘e’ and ‘o’ in this example.

My algorithm would go this way

 void find_duplicate(char* str_1, char* str_2, int len1, int len2)
 {
     char c ;

     if(len1 < len2)
     {
        int* idx_1 = new int[len1]; // record elements in little string
        // that are matched in big string
        for(int k = 0 ; k < len1 ; k++)
              idx_1[k] = 0;

        int* idx_2 = new int[len2]; // record if element in str_2 has been 
        // matched already or not
        for(int k = 0 ; k < len2 ; k++)
              idx_2[k] = 0;

        for(int i = 0 ; i < len2 ; i++)
        {     
            c = str_1[i];

            for(int j = 0 ; j < len1 ; j++)
            {
                 if(str_2[j] == c)
                 {
                    if(idx_2[j] == 0) // this element in str_2 has not been matched yet
                    {
                         idx_1[i] = j + 1; // mark ith element in idx as matched in string 2 at pos j
                         idx_2[j] = 1;
                    }
                 }
             }
         }

         // now idx_1 and idx_2 contain matches info, let's remove matches.
         char* str_1_new = new char[len1];
         char* str_2_new = new char[len2];
         int kn = 0;
         for(int k = 0 ; k < len1 ; k++)
         {
            if(idx_1[k] > 0)
            {
                 str_1_new[kn] = str_1[k];
                 kn++;
            }
         }

         kn = 0;
         for(int k = 0 ; k < len2 ; k++)
         {
             if(idx_2[k] > 0)
             {
                 str_2_new[kn] = str_2[k];
                 kn++;
             }
         }
      }
      else
      {
            // same here, switching roles (do it yourself)
       }
  }

i feel my solution is awkward:
– symetry of both cases in first if/else and code duplication
– time complexity: 2*len1*len2 operations for finding duplicates, then len1 + len2 operations for removal
– space complexity: two len1 and two len2 char*.

What if len1 and len2 are not given (with and without resort to STL vector) ?

could you provide your implementation of this algo ?

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-12T12:53:15+00:00Added an answer on June 12, 2026 at 12:53 pm

    First of all, it isn’t substring matching problem – it is problem of finding common characters between two strings.

    Your solution works in O(n*m), where n=len1 and m=len2 in your code. You could easily solve the same problem in O(n+m+c) time by counting characters in each of strings (where c is equal to size of character set). This algorithm is called counting sort.

    Sample code implementing this in your case:

    #include <iostream>
    #include <cstring> // for strlen and memset
    
    const int CHARLEN = 256; //number of possible chars
    
    using namespace std;
    
    // returns table of char duplicates
    char* find_duplicates(const char* str_1, const char* str_2, const int len1, const int len2)
    {
      int *count_1 = new int[CHARLEN];
      int *count_2 = new int[CHARLEN];
      char *duplicates = new char[CHARLEN+1]; // we hold duplicate chars here
      int dupl_len = 0; // length of duplicates table, we insert '\0' at the end
      memset(count_1,0,sizeof(int)*CHARLEN);
      memset(count_2,0,sizeof(int)*CHARLEN);
      for (int i=0; i<len1; ++i)
      {
        ++count_1[str_1[i]];
      }
      for (int i=0; i<len2; ++i)
      {
        ++count_2[str_2[i]];
      }
    
      for (int i=0; i<CHARLEN; ++i)
      {
        if (count_1[i] > 0 && count_2[i] > 0)
        {
          duplicates[dupl_len] = i;
          ++dupl_len;
        }
      }
      duplicates[dupl_len]='\0';
      delete count_1;
      delete count_2;
      return duplicates;
    }
    
    int main()
    {
      const char* str_1 = "foobar";
      const char* str_2 = "xro";
      char* dup =   find_duplicates(str_1, str_2, strlen(str_1), strlen(str_2));
      cout << "str_1: \"" << str_1 << "\" str_2: \"" << str_2 << "\"\n";
      cout << "duplicates: \"" << dup << "\"\n";
      delete dup;
      return 0;
    }
    

    Please note that I am also sorting the output here. If you do not want to do that, you can skip the counting of characters in second string and just start comparing duplicates on the go.

    If you, however, intend to be able to detect multiple duplicates of the same letter (e.g. if “banana” and “arena” should output “aan” instead of “an”), then you can just substract the number of counts in current solution and adjust the output accordingly.

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

Sidebar

Related Questions

Let's say I have two strings like this: XABY XBAY A simple regex that
I have two strings, one a key and one a value, which I would
If I have two strings that are identical in value, is it guaranteed that
Lets say that I have two strings: The System is in halt state. The
I have two types of strings, hello and helloThere . What I want is
If I have two strings .. say string1=Hello Dear c'Lint and string2=Dear .. I
Possible Duplicate: When do you use the “this” keyword? Hello, I understand that the
I have two Strings as follow: String input=<tr><td>Hello world</td></tr>; String output=<body><tr><td>Hello world</td></tr></body>; I want
Here's a simple example of a program that concatenates two strings. #include <stdio.h> void
Lets say I have two strings: string s1 = hello; string s2 = hello

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.