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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:37:59+00:00 2026-06-12T08:37:59+00:00

Possible Duplicate: What is the complexity of std::string::substr member function? I am attempting a

  • 0

Possible Duplicate:
What is the complexity of std::string::substr member function?

I am attempting a naive solution to the common substring problem. I do this by first finding all the substrings of the first string and adding them to a hash set. Then, I find the substrings of the second string and see if they exist in the hash set, and adjust the longest substring if the length is greater than the current max substring. What I was wondering is: what is the runtime of substr, in terms of the length of the str1 (let’s say str1 has length n)?

#include<hash_set>

string longestCommonSubstring(string str1, string str2) {
    hash_set<string> substrings;
    string maxSubstring = "";
    int maxLength = 0;

    for (int i = 0; i < str1.size(); i++) {
        for (int j = i+1; j < str1.size(); j++) {
            substrings.insert(str1.substr(i,j-i));
        }
    }

    for (int i = 0; i < str2.size(); i++) {
        for (int j = i+1; j < str2.size(); j++) {
            if (substrings.find(str2.substr(i,j-i)) != substrings.end()) {
                if (j-i > maxLength) {
                    maxSubstring = str2.substr(i,j-1);
                    maxLength = j - i;
                }
            }
        }
    }
}
  • 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-12T08:38:00+00:00Added an answer on June 12, 2026 at 8:38 am

    There isn’t a specified complexity for substr, but string has constant-time random access, and substr makes a new string of the given length, so it’ll be essentially the cost of copying that many characters.

    Note that GCC implements std::string with reference counting (illegally so), so the actual cost of making a copy may vary depending on your implementation.

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

Sidebar

Related Questions

Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: What is the cost / complexity of a String.indexof() function call What
Possible Duplicate: std::string and its automatic memory resizing I am just curious, how are
Possible Duplicate: What's the @ in front of a string in C#? This is
Possible Duplicate: how do i access static member of a class? I have this
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:
Possible Duplicate: Are there any O(1/n) algorithms? Any algorithm whose complexity decreases with increase
Possible Duplicate: Are there any O(1/n) algorithms? This just popped in my head for
Possible Duplicate: When should I make explicit use of the this pointer? I'm wondering
Possible Duplicate: Counting inversions in an array This is an phone interview question :

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.