-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckAB.java
35 lines (30 loc) · 864 Bytes
/
checkAB.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*Check AB
Suppose you have a string, S, made up of only 'a's and 'b's. 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.*/
package Recursion;
public class checkAB
{
public static boolean checkAb(String s)
{
if (s.length()==0) {
return true;
}
if (s.charAt(0) != 'a'){
return false;
}
if (s.length() >= 3 && "abb".equals(s.substring(0,3))){
return checkAb(s.substring(3));
}
else
{
return checkAb(s.substring(1));
}
}
public static void main(String[] args)
{
System.out.println(checkAb("abababa"));
}
}