Problem: Construct a deterministic finite automata (DFA) for accepting the language L = {w | w ∈ {a,b}* and Na(w) mod 3 = Nb (w) mod 3}.
Language L={w | Na(w) = Nb(w)mod 3} which means all string contain modulus of count of a’s equal to modulus of count of b’s by 3.
Examples:
Input: a a b b b
Output: NOT ACCEPTED
// n = 2, m=3 (2 mod 3! = 3 mod 3)Input: a a b b
Output: ACCEPTED
// n = 2, m = 2 (2 mod 3 = 2 mod 3)Input: b b b
Output: NOT ACCEPTED
// n = 0, m = 3 ((0 mod 3 = 3 mod 3) <=> (0=0))
Approaches:
- Construct FA for {w | w
{a, b}} and Na = 0 mod 3 means having a’s as a multiple of 3. - Construct FA for {w | w
{a, b}} and Nb = 0 mod 3 means having b’s as a multiple of 3. - Concatenate the two FA and make single DFA using Concatenation process in DFA.
- Making state as final state which accepts the equal modulus count for a’s and b’s.
DFA State Transition Diagram:

States 00, 11 and 22 leads to the acceptance of the string.Whereas states 01, 02, 10, 12, 20 and 21 leads to the rejection of the string.
Let’s see code for the demonstration:
C/C++
// C/C++ Program to construct a DFA which accept the language// L = { w | w is in {a, b}* and Na(w) mod 3 = Nb(w) mod 3}#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 (00)of DFAvoidstart(charc){if(c =='a') {dfa = 10;}elseif(c =='b') {dfa = 1;}// -1 is used to check for any invalid symbolelse{dfa = -1;}}// This function is for the first state (01) of DFAvoidstate01(charc){if(c =='a') {dfa = 11;}elseif(c =='b') {dfa = 2;}else{dfa = -1;}}// This function is for the second state (02) of DFAvoidstate02(charc){if(c =='b') {dfa = 0;}elseif(c =='a') {dfa = 12;}else{dfa = -1;}}// This function is for the third state (10)of DFAvoidstate10(charc){if(c =='b') {dfa = 11;}elseif(c =='a') {dfa = 20;}else{dfa = -1;}}// This function is for the forth state (11)of DFAvoidstate11(charc){if(c =='b') {dfa = 12;}elseif(c =='a') {dfa = 21;}else{dfa = -1;}}voidstate12(charc){if(c =='b') {dfa = 10;}elseif(c =='a') {dfa = 22;}else{dfa = -1;}}voidstate20(charc){if(c =='b') {dfa = 21;}elseif(c =='a') {dfa = 0;}else{dfa = -1;}}voidstate21(charc){if(c =='b') {dfa = 22;}elseif(c =='a') {dfa = 1;}else{dfa = -1;}}voidstate22(charc){if(c =='b') {dfa = 20;}elseif(c =='a') {dfa = 2;}else{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)state01(str[i]);elseif(dfa == 2)state02(str[i]);elseif(dfa == 10)state10(str[i]);elseif(dfa == 11)state11(str[i]);elseif(dfa == 12)state12(str[i]);elseif(dfa == 20)state20(str[i]);elseif(dfa == 21)state21(str[i]);elseif(dfa == 22)state22(str[i]);elsereturn0;}if(dfa == 0 || dfa == 11 || dfa == 22)return1;elsereturn0;}// driver codeintmain(){charstr[] ="aaaabbbb";if(isAccepted(str))printf("ACCEPTED");elseprintf("NOT ACCEPTED");return0;}// This code is contributed by SHUBHAMSINGH10.Java
// Java Program to construct a DFA which accept the language// L = { w | w is in {a, b}* and Na(w) mod 3 = Nb(w) mod 3}classGFG{// dfa tells the number associated// string end in which state.staticintdfa =0;// This function is for// the starting state (00)of DFAstaticvoidstart(charc){if(c =='a') {dfa =10;}elseif(c =='b') {dfa =1;}// -1 is used to check for any invalid symbolelse{dfa = -1;}}// This function is for the first state (01) of DFAstaticvoidstate01(charc){if(c =='a') {dfa =11;}elseif(c =='b') {dfa =2;}else{dfa = -1;}}// This function is for the second state (02) of DFAstaticvoidstate02(charc){if(c =='b') {dfa =0;}elseif(c =='a') {dfa =12;}else{dfa = -1;}}// This function is for the third state (10)of DFAstaticvoidstate10(charc){if(c =='b') {dfa =11;}elseif(c =='a') {dfa =20;}else{dfa = -1;}}// This function is for the forth state (11)of DFAstaticvoidstate11(charc){if(c =='b') {dfa =12;}elseif(c =='a') {dfa =21;}else{dfa = -1;}}staticvoidstate12(charc){if(c =='b') {dfa =10;}elseif(c =='a') {dfa =22;}else{dfa = -1;}}staticvoidstate20(charc){if(c =='b') {dfa =21;}elseif(c =='a') {dfa =0;}else{dfa = -1;}}staticvoidstate21(charc){if(c =='b') {dfa =22;}elseif(c =='a') {dfa =1;}else{dfa = -1;}}staticvoidstate22(charc){if(c =='b') {dfa =20;}elseif(c =='a') {dfa =2;}else{dfa = -1;}}staticbooleanisAccepted(String st){// store length of stringinti, len = st.length();char[] str = st.toCharArray();for(i =0; i < len; i++) {if(dfa ==0)start(str[i]);elseif(dfa ==1)state01(str[i]);elseif(dfa ==2)state02(str[i]);elseif(dfa ==10)state10(str[i]);elseif(dfa ==11)state11(str[i]);elseif(dfa ==12)state12(str[i]);elseif(dfa ==20)state20(str[i]);elseif(dfa ==21)state21(str[i]);elseif(dfa ==22)state22(str[i]);elsereturnfalse;}if(dfa ==0|| dfa ==11|| dfa ==22)returntrue;elsereturnfalse;}// driver codepublicstaticvoidmain(String []args){String str ="aaaabbbb";if(isAccepted(str))System.out.println("ACCEPTED");elseSystem.out.println("NOT ACCEPTED");}}// This code contributed by Rajput-JiPython 3
"""Python Program to construct a DFA which accept the languageL = {w | w belongs to {a, b}*}and Na(w)mod3 = Nb(w)mod 3"""# This function is for# the starting state (00)of DFAdefstart( c):if(c=='a'):dfa=10elif(c=='b') :dfa=1#-1 is used to check for any invalid symbolelse:dfa=-1returndfa# This function is for the first state (01) of DFAdefstate01( c):if(c=='a'):dfa=11elif(c=='b') :dfa=2else:dfa=-1returndfa# This function is for the second state (02) of DFAdefstate02( c):if(c=='b') :dfa=0elif(c=='a'):dfa=12else:dfa=-1returndfa# This function is for the third state (10)of DFAdefstate10( c):if(c=='b') :dfa=11elif(c=='a'):dfa=20else:dfa=-1returndfa# This function is for the forth state (11)of DFAdefstate11( c):if(c=='b') :dfa=12elif(c=='a'):dfa=21else:dfa=-1returndfadefstate12( c):if(c=='b') :dfa=10elif(c=='a'):dfa=22else:dfa=-1returndfadefstate20( c):if(c=='b') :dfa=21elif(c=='a'):dfa=0else:dfa=-1returndfadefstate21( c):if(c=='b') :dfa=22elif(c=='a'):dfa=1else:dfa=-1returndfadefstate22( c):if(c=='b') :dfa=20elif(c=='a'):dfa=2else:dfa=-1returndfadefisAccepted(str):# store length of stringl=len(str)# dfa tells the number associated# with the present dfa = statedfa=0foriinrange(l):if(dfa==0) :start(str[i])elif(dfa==1):state01(str[i])elif(dfa==2) :state02(str[i])elif(dfa==10) :state10(str[i])elif(dfa==11) :state11(str[i])elif(dfa==12) :state12(str[i])elif(dfa==20) :state20(str[i])elif(dfa==21) :state21(str[i])elif(dfa==22) :state22(str[i])else:return0if(dfa==0ordfa==11ordfa==22) :return1else:return0# Driver codeif__name__=="__main__":string="aaaabbbb"if(isAccepted(string)) :print("ACCEPTED")else:print("NOT ACCEPTED")# This code is contributed by SHUBHAMSINGH10.C#
// C# Program to construct a DFA which accept the language// L = { w | w is in {a, b}* and Na(w) mod 3 = Nb(w) mod 3}usingSystem;classGFG{// DFA tells the number associated// string end in which state.staticintdfa = 0;// This function is for// the starting state (00)of DFAstaticvoidstart(charc){if(c =='a') {dfa = 10;}elseif(c =='b') {dfa = 1;}// -1 is used to check for any invalid symbolelse{dfa = -1;}}// This function is for the first state (01) of DFAstaticvoidstate01(charc){if(c =='a') {dfa = 11;}elseif(c =='b') {dfa = 2;}else{dfa = -1;}}// This function is for the second state (02) of DFAstaticvoidstate02(charc){if(c =='b') {dfa = 0;}elseif(c =='a') {dfa = 12;}else{dfa = -1;}}// This function is for the third state (10)of DFAstaticvoidstate10(charc){if(c =='b') {dfa = 11;}elseif(c =='a') {dfa = 20;}else{dfa = -1;}}// This function is for the forth state (11)of DFAstaticvoidstate11(charc){if(c =='b') {dfa = 12;}elseif(c =='a') {dfa = 21;}else{dfa = -1;}}staticvoidstate12(charc){if(c =='b') {dfa = 10;}elseif(c =='a') {dfa = 22;}else{dfa = -1;}}staticvoidstate20(charc){if(c =='b') {dfa = 21;}elseif(c =='a') {dfa = 0;}else{dfa = -1;}}staticvoidstate21(charc){if(c =='b') {dfa = 22;}elseif(c =='a') {dfa = 1;}else{dfa = -1;}}staticvoidstate22(charc){if(c =='b') {dfa = 20;}elseif(c =='a') {dfa = 2;}else{dfa = -1;}}staticBoolean isAccepted(String st){// store length of stringinti, len = st.Length;char[] str = st.ToCharArray();for(i = 0; i < len; i++) {if(dfa == 0)start(str[i]);elseif(dfa == 1)state01(str[i]);elseif(dfa == 2)state02(str[i]);elseif(dfa == 10)state10(str[i]);elseif(dfa == 11)state11(str[i]);elseif(dfa == 12)state12(str[i]);elseif(dfa == 20)state20(str[i]);elseif(dfa == 21)state21(str[i]);elseif(dfa == 22)state22(str[i]);elsereturnfalse;}if(dfa == 0 || dfa == 11 || dfa == 22)returntrue;elsereturnfalse;}// Driver codepublicstaticvoidMain(String []args){String str ="aaaabbbb";if(isAccepted(str))Console.WriteLine("ACCEPTED");elseConsole.WriteLine("NOT ACCEPTED");}}// This code is 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.


