TITLE:-
Check AB
PROBLEM:-
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'
String S
'true' or 'false'
0 <= |S| <= 1000
where |S| represents length of string S.
abb
true
abababa
false
SOLUTION:-
#include <iostream> using namespace std;
bool checkAB(char input[]) {
if(input[0]=='a')
{
if(input[1]=='\0')
return true;
else if(input[1]=='a')
return checkAB(input+1);
else if(input[1]=='b'&&input[2]=='b')
{
if(input[3]=='\0')
return true;
else if(input[3]=='a')
return checkAB(input+3);
else
return false;
}
else
return false;
}
else
return false;
}
int main() {
char input[100];
bool ans;
cin >> input;
ans=checkAB(input);
if(ans)
cout<< "true" << endl;
else
cout<< "false" << endl;
}
This is dummy text. It is not meant to be read. Accordingly, it is difficult to figure out when to end it. But then, this is dummy text. It is not meant to be read. Period.
If you have any doubts then please let me know... ConversionConversion EmoticonEmoticon