#Backtracking Time Limit exceeded issue for Additive numbers.

3 messages · Page 1 of 1 (latest)

reef obsidian
#

i am trying to solve additive number
case: "4012212782529046568"

4012+21278=25290 46568
21278+25290=46568
this is the core idea of the question.
a+b==c you have to verify then
a=b
b=c
then get new c and repeat a+b==c kind of sort of thing on the given string
string length is like 1<=len<=35
so my typical backtracking approach is giving correct answer for smaller working cases but not for larger ones.

so my doubt is will it give correct output for larger input even if it takes a lot of time?
how can i optimize it?
what logic am i missing here.

Below is the code for your Reference

#
class Solution {
        String s;
        int n;
        boolean res=false;
        public boolean isAdditiveNumber(String num) {
            s=num;
            n=s.length();
            res=false;
            if(s.length()<3)
            {
                return false;
            }
            
            bt(0,1,1,2,2,3);
            return res;
        }


        public void bt(int as,int ae,int bs,int be,int cs,int ce)
        {
            
#
if((as<=n)&&(ae<=n)&&(bs<=n)&&(be<=n)&&(cs<=n)&&(ce<=n))
            {
                String a=s.substring(as,ae);
                String b=s.substring(bs,be);
                String c=s.substring(cs,ce);
                System.out.println("a = "+a+" b = "+b+" c ="+c);
               if(a.matches("^0+[0-9]*[1-9]$")|| b.matches("^0+[0-9]*[1-9]$")|| c.matches("^0+[0-9]*[1-9]$"))
                {
                    return;
                }
                else
                {
                    if((Long.parseLong(a)+Long.parseLong(b))==Long.parseLong(c))
                    {
                    //    System.out.println("yess");
                        if(ce==n)
                        {
                            res=true;
                            return;
                        }
                        as=bs;
                        ae=be;
                        bs=cs;
                        be=ce;
                        cs=ce;
                        ce=(ce+1);
                        for(int i=ce;i<=n;i++)
                        {

                            if((Long.parseLong(s.substring(as,ae))+Long.parseLong(
                                s.substring(bs,be)
                            ))<=Long.parseLong(s.substring(cs,i)) )
                            {
                                bt(as,ae,bs,be,cs,i);
                            }
                            
                        }
                    }
                    else {
                        int i=0;
                        int j=0;
                        int k=0;
                        
                        bt(as,ae+1,bs+1,be+1,cs+1,ce+1);
                        bt(as,ae,bs,be+1,cs+1,ce+1);
                        bt(as,ae,bs,be+1,cs,ce+1);
                        bt(as,ae,bs,be,cs,ce+1);
                    }
                }
            }
        }
    }