fork download
  1. class Ideone
  2. {
  3. public static void main (String[] args) throws java.lang.Exception
  4. {
  5. String tests[] = {"Fg", "Xij", "AXij", "Dklm", "AAg", "AA", "Am", "Amp", "Amng", "AFADFophAAQij", "AFADFphAAQij"};
  6.  
  7. for (String test: tests) {
  8. System.out.println(checkGrammar(test));
  9. }
  10. }
  11.  
  12. public static boolean checkGrammar(String input) {
  13. String prev = input;
  14. while (!(input = input.replaceAll("A[g-p]|[FDQX][g-p]{2}", "g")).equals(prev)) {
  15. prev = input;
  16. }
  17.  
  18. return input.matches("[g-p]");
  19. }
  20. }
Success #stdin #stdout 0.08s 380224KB
stdin
Standard input is empty
stdout
false
true
true
false
true
false
true
false
false
true
false