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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:01:16+00:00 2026-05-24T00:01:16+00:00

I have two strings str1 and str2 . Is there any algorithm that can

  • 0

I have two strings str1 and str2.
Is there any algorithm that can be used in order to print out all interleavings of the two strings using recursion?

Update:

public class Interleave {


    private String resultString[] = new String[10];
    private String[] interStr(String str1, String str2){
    int n = ((Factorial.factorial(str1.length() + str2.length())) / (Factorial.factorial(str1.length()) * Factorial.factorial(str2.length())));
    //n is number of interleavings based on (str1.length()+str2.length())! / (str1.length()! * str2.length()!)
    if(str1.length() == 0){
        resultString[0] = str2;
        return resultString;
    }

    if(str2.length() == 0){
        resultString[0] = str1;
        return resultString;
    }

    else{
        for(int i = 0; i < n; i++){
            resultString[i]= str1.substring(0, 1) + interStr(str1.substring(1), str2.substring(1));

        }
    }
    return resultString;
}

    public static void main(String[] args) {
    Interleave obj = new Interleave();
    obj.interStr("12", "abc");
    for(int i = 0; i < obj.resultString.length; i ++){
        System.out.println(obj.resultString[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-05-24T00:01:17+00:00Added an answer on May 24, 2026 at 12:01 am

    The question simply asked whether a recursive algorithm exists for the problem, and the answer is yes. To find it, look for the base case and then for the “step”.

    The base case is when one of the two strings are empty:

    • interleave(s1, "") = {s1}

    • interleave("", s2) = {s2}

    Notice the order of the arguments doesn’t really matter, because

    • interleave("ab", "12") = {“ab12”, “a1b2”, “1ab2”, “a12b”, “1a2b”, “12ab”} = interleave("12", "ab")

    So since the order doesn’t matter we’ll look at recursing on the length of the first string.

    Okay so let’s see how one case leads to the next. I’ll just use a concrete example, and you can generalize this to real code.

    • interleave("", "abc") = {“abc”}
    • interleave("1", "abc") = {“1abc”, “a1bc”, “ab1c”, “abc1”}
    • interleave("12", "abc") = {“12abc”, “1a2bc”, “1ab2c”, “1abc2”, “a12bc”, “a1b2c”, “a1bc2”, “ab12c”, “ab1c2” “abc12”}

    So everytime we added a character to the first string, we formed the new result set by adding the new character to all possible positions in the old result set. Let’s look at exactly how we formed the third result above from the second. How did each element in the second result turn into elements in the third result when we added the “2”?

    • “1abc” => “12abc”, “1a2bc”, “1ab2c”, “1abc2”
    • “a1bc” => “a12bc”, “a1b2c”, “a1bc2”
    • “ab1c” => “ab12c”, “ab1c2”
    • “abc1” => “abc12”

    Now look at things this way:

    • “1abc” => {1 w | w = interleave(“2”, “abc”)}
    • “a1bc” => {a1 w | w = interleave(“2”, “bc”)}
    • “ab1c” => {ab1 w | w = interleave(“2”, “c”)}
    • “abc1” => {abc1 w | w = interleave(“2”, “”)}

    Although one or two examples doesn’t prove a rule in general, in this case you should be able to infer what the rule is. You will have a loop, with recursive calls inside it.

    This is actually a little more fun to do with pure functional programming, but you tagged the question with Java.

    Hopefully this is a start for you. If you get stuck further you can do a web search for “interleaving strings” or “interleaving lists”. There are some solutions out there.

    EDIT:

    Okay I just wrote the silly thing! It’s a lot of fun to write these things in scripting languages, so I thought it would be great to see what it looked like in Java. Not as bad as I thought it would be! Here it is, packaged as an entire Java application.

    import java.util.ArrayList;
    import java.util.List;
    
    public class Interleaver {
    
        /**
         * Returns a list containing all possible interleavings of two strings.
         * The order of the characters within the strings is preserved.
         */
        public static List<String> interleave(String s, String t) {
            List<String> result = new ArrayList<String>();
            if (t.isEmpty()) {
                result.add(s);
            } else if (s.isEmpty()) {
                result.add(t);
            } else {
                for (int i = 0; i <= s.length(); i++) {
                    char c = t.charAt(0);
                    String left = s.substring(0, i);
                    String right = s.substring(i);
                    for (String u : interleave(right, t.substring(1))) {
                        result.add(left + c + u);
                    }
                }
            }
            return result;
        }
    
        /**
         * Prints some example interleavings to stdout.
         */
        public static void main(String[] args) {
            System.out.println(interleave("", ""));
            System.out.println(interleave("a", ""));
            System.out.println(interleave("", "1"));
            System.out.println(interleave("a", "1"));
            System.out.println(interleave("ab", "1"));
            System.out.println(interleave("ab", "12"));
            System.out.println(interleave("abc", "12"));
            System.out.println(interleave("ab", "1234"));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.