Given a string S, made up of only 'a's and 'b's. Check the string for the following rules. Program in python using recursion.
- The string begins with an 'a'
- Each 'a' is followed by nothing or an 'a' or "bb"
- Each "bb" is followed by nothing or an 'a'
I've tried the following code.
def checkAB(s):
if len(s) == 0:
return 1
if s[0] == 'a':
if s[1] == 'a' or s[1] == 'b':
return checkAB(s[2:])
if s[1] == None:
return 1
else:
return 0
elif s[0] == 'b':
if s[1] == 'b' and s[2] == None:
return checkAB(s[2:])
elif s[1] == 'b' and s[2] == 'a':
return 1
else:
return 0
s = input()
if s[0] == 'a':
if checkAB(s[1:]):
print('true')
else:
print('false')
else:
print('false')
But it shows string index out of range error.
Thanks in advance!