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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:35:14+00:00 2026-06-02T20:35:14+00:00

New CS student, studying for a final. I am trying to figure out how

  • 0

New CS student, studying for a final. I am trying to figure out how many times a recursive method will be called in general. Added the code as an example. If I input abcd and efgh, how many calls based on the size of the Strings? If n is any data size, the # of calls is n(?) in any recursive method.

public static String interweave(String s1, String s2)
{ 
   if (s1.equals("") ) return s2;
   else if (s2.equals("")) return s1;
   else return "" + interweave(s1.substring(0,s1.length()-1), s2.substring(0,s2.length()-1))
       +s1.charAt(s1.length()-1)+s2.charAt(s2.length()-1);
}
  • 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-02T20:35:16+00:00Added an answer on June 2, 2026 at 8:35 pm

    Notice that in your question, at each recursive step you reduce by one the size of both strings until either one of them has length zero (the base cases of the recursion). It’s easy to see that the number of recursive calls is min(m, n) + 1, where m is the initial length of s1 and n is the initial length of s2.

    For example, if s1 = "abc" and s2 = "de" it will take 2 recursive calls to traverse s2 (the string with the minimum length) plus one extra call for exiting at the base case, therefore min(s1.length(), s2.length()) + 1 == 3. You can test it programmatically like this:

    static int counter;
    
    public static String interweave(String s1, String s2) {
        counter++;
        if (s1.equals(""))
            return s2;
        else if (s2.equals(""))
            return s1;
        else
            return interweave(s1.substring(0, s1.length()-1), s2.substring(0, s2.length()-1))
            + s1.charAt(s1.length()-1)+s2.charAt(s2.length()-1);
    }
    
    public static int count(String s1, String s2) {
        counter = 0;
        interweave(s1, s2);
        return counter;
    }
    

    Now when you run the following statements, the formula works as expected:

    // s1.length() == s2.length()
    System.out.println(count("abcde", "fghij"));
    > 6
    
    // s1.length() > s2.length()
    System.out.println(count("abcde", "fg"));
    > 3
    
    // s1.length() < s2.length()
    System.out.println(count("ab", "cdefg"));
    > 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to add 1 to any newly appended id/class: jQuery: $(function(){ $(#add-new-student).click(function()
public static ArrayList GetStudentAsArrayList() { ArrayList students = new ArrayList { new Student() {
So, I'm a brand new CS student, on a Mac, and I'm learning C++
EDIT : proper solution: void add(Student s) { if(root == null) root = new
I'm very new to Web server matters, and relatively a naive student of C++.
I have two classes Teacher and Student . I am trying to get teacher
This is probably a very easy question. Hey I'm a student and relatively new
I am currently a CS student at a 4th year(of total 5). I`m studying
I have following setup. method and method1 both are public methods of class Student.
I am trying to create an array of pointers. These pointers will point to

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.