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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:47:26+00:00 2026-05-15T10:47:26+00:00

i have 2 sequences, for instance s=aaba and ss=aa, and i want all the

  • 0

i have 2 sequences, for instance s=aaba and ss=aa, and i want all the way ss is in s.
In this example:
[0,1], [0,3] and [1,3]
My code is below. It works fine, except for very long s with multiple ss. In that case i’ve got

Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded
(I already use java with -Xmx at the maximum I can…)

public static ArrayList<ArrayList<Integer>> getListIndex(String[] s, String[] ss, int is, int iss) {
    ArrayList<ArrayList<Integer>> listOfListIndex = new ArrayList<ArrayList<Integer>>();
    ArrayList<ArrayList<Integer>> listRec = new ArrayList<ArrayList<Integer>>();
            ArrayList<Integer> listI = new ArrayList<Integer>();

    if (iss<0||is<iss){
        return listOfListIndex;
    }

    if (ss[iss].compareTo(s[is])==0){

        //ss[iss] matches, search ss[0..iss-1] in s[0..is-1]
        listRec = getListIndex(s,ss,is-1,iss-1);

        //empty lists (iss=0 for instance)
        if(listRec.size()==0){
            listI = new  ArrayList<Integer>();
            listI.add(is);
            listOfListIndex.add(listI);
        }
        else{
            //adding to what we have already found
            for (int i=0; i<listRec.size();i++){
                listI = listRec.get(i);
                    listI.add(is);
                    listOfListIndex.add(listI);
            }
        }
    }
    //In all cases
    //searching ss[0..iss] in s[0..is-1]
    listRec = getListIndex(s,ss,is-1,iss);
    for (int i=0; i<listRec.size();i++){
        listI = listRec.get(i);
            listOfListIndex.add(listI);
    }

    return listOfListIndex;
}   

Is there anyway to do this more efficiently ?

  • 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-15T10:47:27+00:00Added an answer on May 15, 2026 at 10:47 am

    I doubt the recursion is the problem (think of what the maximum recursion depth is). The algorithm can be efficiently implemented by collecting the indecies of each character of s in ss in TreeSets and then simply taking the .tailSet when needing to “advance” in the string.

    import java.util.*;
    
    
    public class Test {
    
        public static Set<List<Integer>> solutions(List<TreeSet<Integer>> is, int n) {
    
            TreeSet<Integer> ts = is.get(0);
            Set<List<Integer>> sol = new HashSet<List<Integer>>();
            for (int i : ts.tailSet(n+1)) {
                if (is.size() == 1) {
                    List<Integer> l = new ArrayList<Integer>();
                    l.add(i);
                    sol.add(l);
                } else 
                    for (List<Integer> tail : solutions(is.subList(1, is.size()), i)) {
                        List<Integer> l = new ArrayList<Integer>();
                        l.add(i);
                        l.addAll(tail);
                        sol.add(l);
                    }
            }
            return sol;
        }
    
    
        public static void main(String[] args) {
            String ss = "aaba";
            String s = "aa";
    
            List<TreeSet<Integer>> is = new ArrayList<TreeSet<Integer>>();
    
            // Compute all indecies of each character.
            for (int i = 0; i < s.length(); i++) {
                TreeSet<Integer> indecies = new TreeSet<Integer>();
                char c = s.charAt(i);
                for (int j = 0; j < ss.length(); j++) {
                    if (ss.charAt(j) == c)
                        indecies.add(j);
                }
                is.add(indecies);
            }
    
            System.out.println(solutions(is, -1));
        }
    }
    

    Output:

    [[0, 1], [1, 3], [0, 3]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have very long integer sequences that look like this (arbitrary length!): 0000000001110002220033333 Now
I am working with DNA sequences of length 25 (see examples below). I have
Below is a clip from a listing of two Pentium assembly sequences. We have
I have a question regarding postgresql sequences. For instance, for bigserial datatype, is it
I have a schema like below, the color elements of default and instance have
I have some parsing code that allows for escape sequences to be entered into
Let's say we have a data structure like this: var sequences = new List<Tuple<int,
I have the following XML: <?xml version=1.0?> <coll xmlns=http://www.example.org/coll xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation=http://www.example.org/coll coll.xsd> <collection> <item
Specific instance of Problem I have an int range from 1-100. I want to
I have a method where I want to factor out some code into its

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.