Construct a DFA which accept the language L = {anbm | n > =1, (m) mod 3 = 1}
Problem: Construct a DFA which accept the language L = {anbm | n > =1, (m) mod 3 = 1}.
Explanation:
For constructing the DFA, the following things to be remember:
which means any no of elements, and
=
which means any no of elements greater than 1.
Examples:
Input: a a b b b Output: NOT ACCEPTED // n = 2 (>=1), m=3 ((3) mod3 != 1) Input: a a a b Output: ACCEPTED // n = 3 (>=1), m = 1 ((1) mod 3= 1) Input: b b b b Output: NOT ACCEPTED // n = 0(must be >=1), m = 4 ((4) mod 3 = 1)
Approaches:
It’s construction should contain the following steps:
- Step-1: Construct FA for
means having any number of a’s greater than one. - Step-2: Construct FA for
means having exactly - Step-3: Construct FA for
means having b’s equal to multiple of 3 . - Step-4: Concatenate the three FA and make single DFA. Always maintain the order of a, b and c.
Given DFA has following states. State 2 leads to the acceptance of the string.Whereas states 0, 1, 3, 4 and 5 leads to the rejection of the string.
DFA State Transition Diagram:

Let’s see code for the demonstration:
C/C++
// C program to implement DFS that accepts// all string which follow the language// L = { a^n b^m ; n >=1, (m)mod 3=1}#include <stdio.h>#include <string.h>// dfa tells the number associated// string end in which state.intdfa = 0;// This function is for// the starting state (Q0)of DFAvoidstart(charc){if(c =='a') {dfa = 1;}elseif(c =='b') {dfa = 5;}// -1 is used to check for any invalid symbolelse{dfa = -1;}}// This function is for the first state (Q1) of DFAvoidstate1(charc){if(c =='a') {dfa = 1;}elseif(c =='b') {dfa = 2;}else{dfa = -1;}}// This function is for the second state (Q2) of DFAvoidstate2(charc){if(c =='b') {dfa = 3;}elseif(c =='a') {dfa = 5;}else{dfa = -1;}}// This function is for the third state (Q3)of DFAvoidstate3(charc){if(c =='b') {dfa = 4;}elseif(c =='a') {dfa = 5;}else{dfa = -1;}}// This function is for the forth state (Q4)of DFAvoidstate4(charc){if(c =='b') {dfa = 2;}elseif(c =='a') {dfa = 5;}else{dfa = -1;}}// This function is for the fifth state (Q5) of DFAvoidstate5(charc){dfa = -1;}intisAccepted(charstr[]){// store length of stringinti, len =strlen(str);for(i = 0; i < len; i++) {if(dfa == 0)start(str[i]);elseif(dfa == 1)state1(str[i]);elseif(dfa == 2)state2(str[i]);elseif(dfa == 3)state3(str[i]);elseif(dfa == 4)state4(str[i]);elseif(dfa == 5)state5(str[i]);elsereturn0;}if(dfa == 2)return1;elsereturn0;}// driver codeintmain(){charstr[] ="aaabbbb";if(isAccepted(str))printf("ACCEPTED");elseprintf("NOT ACCEPTED");return0;}// This code is contributed by SHUBHAMSINGH10.Java
// Java program to implement DFS that accepts// all string which follow the language// L = { a^n b^m ; n >=1, (m)mod 3=1}importjava.util.*;classGFG {// dfa tells the number associated// string end in which state.staticintdfa =0;// This function is for// the starting state (Q0)of DFAstaticvoidstart(charc){if(c =='a') {dfa =1;}elseif(c =='b') {dfa =5;}// -1 is used to check for any invalid symbolelse{dfa = -1;}}// This function is for the first state (Q1) of DFAstaticvoidstate1(charc){if(c =='a') {dfa =1;}elseif(c =='b') {dfa =2;}else{dfa = -1;}}// This function is for the second state (Q2) of DFAstaticvoidstate2(charc){if(c =='b') {dfa =3;}elseif(c =='a') {dfa =5;}else{dfa = -1;}}// This function is for the third state (Q3)of DFAstaticvoidstate3(charc){if(c =='b') {dfa =4;}elseif(c =='a') {dfa =5;}else{dfa = -1;}}// This function is for the forth state (Q4)of DFAstaticvoidstate4(charc){if(c =='b') {dfa =2;}elseif(c =='a') {dfa =5;}else{dfa = -1;}}// This function is for the fifth state (Q5) of DFAstaticvoidstate5(charc){dfa = -1;}staticintisAccepted(charstr[]){// store length of stringinti, len = str.length;for(i =0; i < len; i++) {if(dfa ==0)start(str[i]);elseif(dfa ==1)state1(str[i]);elseif(dfa ==2)state2(str[i]);elseif(dfa ==3)state3(str[i]);elseif(dfa ==4)state4(str[i]);elseif(dfa ==5)state5(str[i]);elsereturn0;}if(dfa ==2)return1;elsereturn0;}// Driver codepublicstaticvoidmain(String[] args) {charstr[] ="aaabbbb".toCharArray();if(isAccepted(str)==1)System.out.println("ACCEPTED");elseSystem.out.println("NOT ACCEPTED");}}// This code is contributed by Rajput-JiPython 3
# Python3 program to implement DFS that accepts# all Stringing which follow the language# L = {a ^ n b ^ m | n >= 1, (m)mod 3 = 1 }# This function is for the dfa = starting# dfa = state (zeroth) of DFAdefstart(c):if(c=='a'):dfa=1elif(c=='b'):dfa=5# -1 is used to check for any# invalid symbolelse:dfa=-1returndfa# This function is for the first# dfa = state of DFAdefstate1(c):if(c=='a'):dfa=1elif(c=='b'):dfa=2else:dfa=-1returndfa# This function is for the second# dfa = state of DFAdefstate2(c):if(c=='b'):dfa=3elif(c=='a'):dfa=5else:dfa=-1returndfa# This function is for the third# dfa = state of DFAdefstate3(c):if(c=='b'):dfa=4elif(c=='a'):dfa=5else:dfa=-1returndfa# This function is for the fouth# dfa = state of DFAdefstate4(c):if(c=='b'):dfa=2elif(c=='a'):dfa=5else:dfa=-1returndfa# This function is for the fifth# dfa = state of DFAdefstate5(c):dfa=-1returndfadefisAccepted(String):# store length of Stringingl=len(String)# dfa tells the number associated# with the present dfa = statedfa=0foriinrange(l):if(dfa==0):dfa=start(String[i])elif(dfa==1):dfa=state1(String[i])elif(dfa==2) :dfa=state2(String[i])elif(dfa==3) :dfa=state3(String[i])elif(dfa==4) :dfa=state4(String[i])elif(dfa==5) :dfa=state5(String[i])else:return0if(dfa==2) :return1else:return0# Driver codeif__name__=="__main__":String="aaabbbb"if(isAccepted(String)) :print("ACCEPTED")else:print("NOT ACCEPTED")# This code is contributed by SHUBHAMSINGH10.C#
// C# program to implement DFS that accepts// all string which follow the language// L = { a^n b^m ; n >=1, (m)mod 3=1}usingSystem;publicclassGFG {// dfa tells the number associated// string end in which state.staticintdfa = 0;// This function is for// the starting state (Q0)of DFAstaticvoidstart(charc){if(c =='a') {dfa = 1;}elseif(c =='b') {dfa = 5;}// -1 is used to check for any invalid symbolelse{dfa = -1;}}// This function is for the first state (Q1) of DFAstaticvoidstate1(charc){if(c =='a') {dfa = 1;}elseif(c =='b') {dfa = 2;}else{dfa = -1;}}// This function is for the second state (Q2) of DFAstaticvoidstate2(charc){if(c =='b') {dfa = 3;}elseif(c =='a') {dfa = 5;}else{dfa = -1;}}// This function is for the third state (Q3)of DFAstaticvoidstate3(charc){if(c =='b') {dfa = 4;}elseif(c =='a') {dfa = 5;}else{dfa = -1;}}// This function is for the forth state (Q4)of DFAstaticvoidstate4(charc){if(c =='b') {dfa = 2;}elseif(c =='a') {dfa = 5;}else{dfa = -1;}}// This function is for the fifth state (Q5) of DFAstaticvoidstate5(charc){dfa = -1;}staticintisAccepted(char[]str){// store length of stringinti, len = str.Length;for(i = 0; i < len; i++) {if(dfa == 0)start(str[i]);elseif(dfa == 1)state1(str[i]);elseif(dfa == 2)state2(str[i]);elseif(dfa == 3)state3(str[i]);elseif(dfa == 4)state4(str[i]);elseif(dfa == 5)state5(str[i]);elsereturn0;}if(dfa == 2)return1;elsereturn0;}// Driver codepublicstaticvoidMain(String[] args) {char[]str ="aaabbbb".ToCharArray();if(isAccepted(str)==1)Console.WriteLine("ACCEPTED");elseConsole.WriteLine("NOT ACCEPTED");}}// This code has been contributed by 29AjayKumarOutput:
ACCEPTED
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.


