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 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 a question regarding postgresql sequences. For instance, for bigserial datatype, is it
I have found this example on StackOverflow: var people = new List<Person> { new
If I have sequence of sequences (maybe a list of tuples) I can use
At work we have an application to play 2K (2048*1556px) OpenEXR film sequences. It
I have a sequence of SQL calls, which I want to use to detect
I have one hibernate sequence, that generates all sequence-numbers in my app. When I
Let us presume we have k sequences of fixed length p . Each sequence
I have a few classes that define sequences whose values must be available both
I have a Random into a class which aims to generate random sequences in
Ruby 1.8.6 I have an array containing numerical values. I want to reduce it

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.