diff --git a/recursion 2/check ab b/recursion 2/check ab index f773e54..22934d4 100644 --- a/recursion 2/check ab +++ b/recursion 2/check ab @@ -1,33 +1,19 @@ -Suppose you have a string made up of only 'a' and 'b'. Write a recursive function that checks if the string was generated using the following rules: -a. The string begins with an 'a' -b. Each 'a' is followed by nothing or an 'a' or "bb" -c. Each "bb" is followed by nothing or an 'a' -If all the rules are followed by the given string, return true otherwise return false. -Sample Input: -abb -Sample Output: -true - -bool solve(char input[]) { - if(input[0]=='\0') - return true; // Write your code here - bool so; - if(input[0]=='a'&&(input[1]=='\0'||input[1]=='a')) - so=solve(input+1); - else if(input[0]=='a'&&(input[1]=='b'&&input[2]=='b')) - so=solve(input+1); - else if(input[0]=='b'&&input[1]=='b'&&(input[2]=='\0'||input[2]=='a')) - so= solve(input+2); - else - return false; - return so; -} - - bool checkAB(char input[]) { - if(input[0]=='\0') - return true; - else if (input[0] != 'a') + + if (input [0] == '\0') + { + return true; + } + + if (input [0] != 'a') + { return false; - return solve(input); + } + + if (input [1] == 'b' && input [2] == 'b') + { + return checkAB (input + 3); + } + + return checkAB (input + 1); }