how can we print the pattern (aa,ab,ba,a,b ) using recursion
Problem in printing the pattern
hi @kingshuk441, if you want to print sequences consisting of a and b and no consiquetive b then you can simply have a character variable in the function parameter that store the previous charater , now in every call you can check for the case that
- if previous character was βaβ then you can have two cases in one in which you set current charater as βaβ and another in which you set your current character as βbβ
- if previous character was b then you canβt have current character as a b thus u have only one case i.e setting current charater as a
If you are storing your string then u donβt need previos character as a parameter then you simpy have to check with string[i-1] if u are at i
try to implement this yourself . in case of any difficulty feel free to ask
mark your doubt as resolved if u got the answer
not able to implement this
Here, i have corrected your code
refer this to see the changes i made in your code
https://www.diffchecker.com/QJ6ks11Q
ask me if you have any doubt in any step
this code is not printing all possible pattern
hi there was a small error :
at line 24 , instead of rec(n-1,x,output,j+1); it should have been rec(n-1,βaβ,output,j+1);
refer this :- https://ide.codingblocks.com/s/213499