Please have a glance at my code. I used StringBuilder instead of String as it was giving time limit, but now I am facing run error in my code.
Class Assignment : Stack OverFlow Error
i thin ur code is not terminating.
what is ur approach?
this is a very classic problem of dp.
I haven’t studied dp yet. Even this question is not expected to be done by using dp.
bro, the qn is how many words we can formed using a’s and b’s of given n length w/o 2 consecutive b’s.
suppose n==1
then we can form 2 strings : a and b
and if n==2
then strings will be: aa, ba, ab (bb can’t be included)
let CNTA be a variable which holds the no. of strings can be formed ending with a
let CNTB be a variable which holds the no. of strings can be formed ending with b
so for n=1 : CNTA = CNTB=1;
for the n=m
for(int i=2;i<=m;i++){
int temp=CNTB;
CNTB=CNTA; // b can be appended to only the string which ends with a only,
CNTA=CNTA+temp; // we can append a to both the strings which ends with a as well as b
}
last me
dono ka sum
will be result
i have written this code in discussion chat box, so there may be some syntax error
hope this will help u
pls rate my work so that i can improve my self
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.