Skip to content

Update check ab #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 16 additions & 30 deletions recursion 2/check ab
Original file line number Diff line number Diff line change
@@ -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);
}