简体   繁体   中英

why does this code gives stackoverflow error when word length is beyond 4?

i have written a code to generate all possible combinations of letters of a word without any repetition of any letter or any particular word. the code is as follows

static boolean redcheck(int array[])// checks if letters are repeated
{
    boolean check=true;
    for(int i=0;i<array.length-1;i++)
    {
        for(int j=i+1;j<array.length;j++)
        {
            if(array[i]==array[j])
            {
                check=false;
                break;
            }
        }
    }
    return check;
}

static void repeat(char arr2[],int arr1[],int p)// creates and prints the word
{
    if(redcheck(arr1))
    {
        for(int i=0;i<p;i++)
            System.out.print(arr2[arr1[i]]);
        for(int i=0;i<p;i++)
            System.out.print(arr1[i]);
        System.out.println();
    }
    arr1[p-1]+=1;
    for(int ini=p-1;ini>0;ini--)
    {  
        if(arr1[ini]>p-1)
        {
            arr1[ini-1]+=1;
            arr1[ini]=0;  
        }
    }
    if(arr1[0]>p-1)
        return;

    repeat(arr2,arr1,p);
}

public static void main()
{
    Scanner sc=new Scanner(System.in);
    System.out.println("enter word");
    String a=sc.nextLine();
    int num=a.length();
    char arr[]=new char[num];
    for(int c=0;c<a.length();c++)
        arr[c]=a.charAt(c);

    int arr1[]=new int[num];
    for(int i:arr1)
        arr1[i]=0;
    repeat(arr,arr1,num);

}

the code works fine till any word of upto length 4, but when its above four, it throws up a stack overflow error. after some inspection, the main part of code which is creating the problem is the printing part itself, which is

for(int i=0;i<p;i++)
            System.out.print(arr2[arr1[i]]);

i really cant find where i am going wrong. the print statement below the one mentioned above, prints the indexes of the words in order which they will be printed and does not give any error. i am using the bluej editor, and it seems i have 512MB in stack memory. please help.thanks in advance.

EDIT: the error code is java.lang.StackOverflowError: null

With 4 letters (assuming none of them are the same) there are 4^4 = 256 possible combinations of these letters. As your code is currently set up, you will recurse at least 256 times before returning a value which will have a big memory cost on your stack. If you try to scale up to 5 letters (once again assuming none are the same) you will have 5^5 = 3125 possible combinations, etc... The stack overflow error you get is due to the amount of time you recurse.

My recommendation: separate your repeat method into two parts:

static void printWord(char arr2[],int arr1[],int p) {
    if(redcheck(arr1))
    {
        for(int i=0;i<p;i++)
            System.out.print(arr2[arr1[i]]);
        for(int i=0;i<p;i++)
            System.out.print(arr1[i]);
        System.out.println();
    }
}

and then your repeat method:

static void repeat(char arr2[],int arr1[],int p)// creates and prints the word
{
    while(arr1[0] < p-1){
        printWord(char arr2[],int arr1[],int p);
        arr1[p-1]+=1; // your looping logic
        for(int ini=p-1;ini>0;ini--)
        {  
            if(arr1[ini]>p-1)
            {
                arr1[ini-1]+=1;
                arr1[ini]=0;  
            }
        }
    }
}

making it non recursive will help you avoid the stack overflow error.

Additional recommendations: Verify if the input word does not have two of the same letters before running any logic, your code will not find any combinations if I input the word "see" as there are no combinations such that a three letter word can be created with the letters {'s','e','e'} without any of them repeating. Your redcheck method uses one too many variables:

static boolean redcheck(int array[])// checks if letters are repeated
{
    for(int i=0;i<array.length-1;i++)
    {
        for(int j=i+1;j<array.length;j++)
        {
            if(array[i]==array[j])
            {
                return false;
            }
        }
    }
    return true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM