Problem Statement: Design a Definite Finite Automata for accepting the permutation of Three a’s and Three b’s over the input {a, b}
Input: S = “aaabbb”
Output: Accepted
Explanation:
The input has three a and three b.Input: S = “abababa”
Output: Accepted
Explanation:
The input has three a and three b.
To design a DFA we need to check the input character by character. These are a few steps that should be kept in mind while designing a DFA.
- Think of all most of the possible input that can be accepted (like, in this example, aaabbb, bbbaaa, ababab, abaabb…etc).
- Create an initial transition state.
- Do transition on each input alphabet to other transition states.
- Reach a final state such that in each step the language used was accept(like, in this example, an equal number of a and b).
Designing steps:
Step 1: Create an initial state “A” which is indicated by —>.
Step 2: Think of the possible string the can aaabbb or bbbaaa which can occur by transition like,
- The transition of input ‘a’ from state “A” to state “B”
- The transition of input ‘a’ from state “B” to state “C”
- The transition of input ‘a’ from state “C” to state “D”
- The transition of input ‘a’ from state “D” to dead state “Q2”
- The transition of input ‘b’ from state “D” to state “E”
- The transition of input ‘b’ from state “E” to state “F”
- The transition of input ‘b’ from state “F” to state “G”
- The transition of input ‘b’ from state “G” to dead state “Q2”

Step 3: Now work on a possibility like b’s comes after two a’s for example aababb, aabbab.
- The transition of ‘b’ from state “C” to state “J”
- The transition of ‘a’ from state “J” to state “E”
- The transition of ‘b’ from state “J” to state “M”
- The transition of ‘a’ from state “M” to state “F”
- The transition of ‘b’ from state “M” to state “P”
The transition is shown in the diagram.

Step-4: Now thinking of the possibility that starts with ‘aba’ and then fulfills the condition of the given language. For this following transitions to be done:
- The transition of ‘b’ from state “B” to state “I”
- The transition of ‘a’ from state “I” to state “J”

Step-5: Now thinking of the possibility that starts with ‘abb’ and then fulfills the condition of the given language. For this following transitions to be done:
- The transition of ‘b’ from state “I” to state “L”
- The transition of ‘a’ from state “L” to state “M”

Step-6: Now there is a possibility that after reading one ‘a’ it reads all 3 b’s and then end execution by reading the remaining 2 a’s. For the transition of ‘b’ from state “L” to “O”.

Step-7: there is a possibility that it reads ‘a’ after reading one ‘b’ and then it reads any combination of ‘a’ and ‘b’ so that string contains 3 a’s and 3 b’s.For this transition of ‘a’ is done from state”H” to state “I”.

Step-8: Now, think about the strings starts with two b’s and the go for any combination of string to get the desired result for this we do a transition of ‘a’ from stare “K” to state “L”.

Step-9: Now input alphabet ‘a’ of states D, E, F, G, and ‘b’ of states G, N, O, P are left for transitions.AS we have done all possibilities of the above language so the remaining transition will go to dead states “Q1″ and Q2”.
- The transition of input ‘a’ from state “D” to dead state “Q1”
- The transition of input ‘a’ from state “E” to dead state “Q1”
- The transition of input ‘a’ from state “F” to dead state “Q1”
- The transition of input ‘a’ from state “G” to dead state “Q1”
- The transition of input ‘b’ from state “G” to dead state “Q2”
- The transition of input ‘b’ from state “N” to dead state “Q2”
- The transition of input ‘b’ from state “O” to dead state “Q2”
- The transition of input ‘b’ from state “P” to dead state “Q2”

Transition table of above DFA
| STATES | INPUT (a) | INPUT (b) |
|---|---|---|
| —> A (initial state) | B | H |
| B | C | I |
| C | D | J |
| D | Q2 (dead state) | E |
| E | Q2 (dead state) | F |
| F | Q2 (dead state) | G*(final state) |
| G* (final state) | Q2 (dead state) | Q1 (dead state) |
| H | I | K |
| I | J | L |
| J | E | M |
| K | L | N |
| L | M | O |
| M | F | P |
| N | O | Q1 (dead state) |
| O | P | Q1 (dead state) |
| P | G* (final state) | Q1 (dead state) |
Below is the implementation of the DFA:
Java
// Java implementation of the // DFA of permutation of three // a's and three b'simport java.util.*;class GFG{ // State Astatic void stateA(String n){ if (n.charAt(0) == 'a') stateB(n.substring(1)); else if (n.charAt(0) == 'b') { stateH(n.substring(1)); } } // State Bstatic void stateB(String n){ if (n.length() == 0) System.out.print("String Not Accepted"); else { if (n.charAt(0) == 'a') stateC(n.substring(1)); else if (n.charAt(0) == 'b') stateI(n.substring(1)); }} // State Cstatic void stateC(String n){ if (n.length() == 0) System.out.print("String Not Accepted"); else { if (n.charAt(0) == 'a') stateD(n.substring(1)); else if (n.charAt(0) == 'b') stateJ(n.substring(1)); }} // State Dstatic void stateD(String n){ if (n.length() == 0) System.out.print("String Not Accepted"); else { if (n.charAt(0) == 'a') stateQ2(n); else if (n.charAt(0) == 'b') stateE(n.substring(1)); }} // State Estatic void stateE(String n){ if (n.length() == 0) System.out.print("String Not Accepted"); else { if (n.charAt(0) == 'a') stateQ2(n); else if (n.charAt(0) == 'b') stateF(n.substring(1)); }} // State Fstatic void stateF(String n){ if (n.length() == 0) System.out.print("String Not Accepted"); else { if (n.charAt(0) == 'a') stateQ2(n.substring(1)); else if (n.charAt(0) == 'b') stateG(n.substring(1)); }} // State G static void stateG(String n){ if (n.length() == 0) System.out.print("String Accepted"); else { if (n.charAt(0) == 'a') stateQ2(n); else if (n.charAt(0) == 'b') stateQ2(n); }} // State Hstatic void stateH(String n){ if (n.length() == 0) System.out.print("String Not Accepted"); else { if (n.charAt(0) == 'a') stateI(n.substring(1)); else if (n.charAt(0) == 'b') stateK(n.substring(1)); }} // State I static void stateI(String n){ if (n.length() == 0) System.out.print("String Not Accepted"); else { if (n.charAt(0) == 'a') stateJ(n.substring(1)); else if (n.charAt(0) == 'b') stateL(n.substring(1)); }} // State Jstatic void stateJ(String n){ if (n.length() == 0) System.out.print("String Not Accepted"); else { if (n.charAt(0) == 'a') stateE(n.substring(1)); else if (n.charAt(0) == 'b') stateM(n.substring(1)); }} // State Kstatic void stateK(String n){ if (n.length() == 0) System.out.print("String Not Accepted"); else { if (n.charAt(0) == 'a') stateL(n.substring(1)); else if (n.charAt(0) == 'b') stateN(n.substring(1)); }} // State Lstatic void stateL(String n){ if (n.length() == 0) System.out.print("String Not Accepted"); else { if (n.charAt(0) == 'a') stateM(n.substring(1)); else if (n.charAt(0) == 'b') stateO(n.substring(1)); }} // State Mstatic void stateM(String n){ if (n.length() == 0) System.out.print("String Not Accepted"); else { if (n.charAt(0) == 'a') stateF(n.substring(1)); else if (n.charAt(0) == 'b') stateP(n.substring(1)); }} // State Nstatic void stateN(String n){ if (n.length() == 0) System.out.print("String Not Accepted"); else if (n.charAt(0) =='a') stateO(n.substring(1)); else if (n.charAt(0) == 'b') stateQ1(n);} // State Qstatic void stateO(String n){ if (n.length() == 0) System.out.print("String Not Accepted"); else { if (n.charAt(0) == 'a') stateP(n.substring(1)); else if (n.charAt(0) == 'b') stateQ1(n); }} // State Pstatic void stateP(String n){ if (n.length() == 0) System.out.print("String Not Accepted"); else { if (n.charAt(0) == 'a') stateG(n.substring(1)); else if (n.charAt(0) == 'b') stateQ1(n.substring(1)); }} // State Q1static void stateQ1(String n){ System.out.print("String Not Accepted");} // State Q2static void stateQ2(String n){ System.out.print("String Not Accepted");} // Driver codepublic static void main(String[] args){ // Take String input String n = "abaabb"; // Call stateA // to check the input stateA(n);} }// This code is contributed by pratham76 |
Python3
# Python3 implementation of the # DFA of permutation of three # a's and three b's# State Adef stateA(n): if(n[0]=='a'): stateB(n[1:]) elif (n[0]=='b'): stateH(n[1:])# State Bdef stateB(n): if(len(n)== 0): print("String Not Accepted") else: if(n[0]=='a'): stateC(n[1:]) elif (n[0]=='b'): stateI(n[1:])# State Cdef stateC(n): if(len(n)== 0): print("String Not Accepted") else: if(n[0]=='a'): stateD(n[1:]) elif (n[0]=='b'): stateJ(n[1:])# State Ddef stateD(n): if(len(n)== 0): print("String Not Accepted") else: if(n[0]=='a'): stateQ2(n) elif (n[0]=='b'): stateE(n[1:]) # State Edef stateE(n): if(len(n)== 0): print("String Not Accepted") else: if(n[0]=='a'): stateQ2(n) elif (n[0]=='b'): stateF(n[1:]) # State Fdef stateF(n): if(len(n)== 0): print("String Not Accepted") else: if(n[0]=='a'): stateQ2(n[1:]) elif (n[0]=='b'): stateG(n[1:]) # State G def stateG(n): if(len(n)== 0): print("String Accepted") else: if(n[0]=='a'): stateQ2(n) elif (n[0]=='b'): stateQ2(n) # State Hdef stateH(n): if(len(n)== 0): print("String Not Accepted") else: if(n[0]=='a'): stateI(n[1:]) elif (n[0]=='b'): stateK(n[1:]) # State I def stateI(n): if(len(n)== 0): print("String Not Accepted") else: if(n[0]=='a'): stateJ(n[1:]) elif (n[0]=='b'): stateL(n[1:]) # State Jdef stateJ(n): if(len(n)== 0): print("String Not Accepted") else: if(n[0]=='a'): stateE(n[1:]) elif (n[0]=='b'): stateM(n[1:]) # State Kdef stateK(n): if(len(n)== 0): print("String Not Accepted") else: if(n[0]=='a'): stateL(n[1:]) elif (n[0]=='b'): stateN(n[1:]) # State Ldef stateL(n): if(len(n)== 0): print("String Not Accepted") else: if(n[0]=='a'): stateM(n[1:]) elif (n[0]=='b'): stateO(n[1:]) # State Mdef stateM(n): if(len(n)== 0): print("String Not Accepted") else: if(n[0]=='a'): stateF(n[1:]) elif (n[0]=='b'): stateP(n[1:]) # State Ndef stateN(n): if(len(n)== 0): print("String Not Accepted") else: if(n[0]=='a'): stateO(n[1:]) elif (n[0]=='b'): stateQ1(n) # State Qdef stateO(n): if(len(n)== 0): print("String Not Accepted") else: if(n[0]=='a'): stateP(n[1:]) elif (n[0]=='b'): stateQ1(n) # State Pdef stateP(n): if(len(n)== 0): print("String Not Accepted") else: if(n[0]=='a'): stateG(n[1:]) elif (n[0]=='b'): stateQ1(n[1:]) # State Q1def stateQ1(n): print("String Not Accepted")# State Q2def stateQ2(n): print("String Not Accepted")# take string inputn = "abaabb"# call stateA# to check the inputstateA(n) |
C#
// C# implementation of the // DFA of permutation of three // a's and three b'susing System;using System.Collections;class GFG{ // State Astatic void stateA(string n){ if(n[0] == 'a') stateB(n.Substring(1)); else if (n[0] == 'b') { stateH(n.Substring(1)); } } // State Bstatic void stateB(string n){ if(n.Length == 0) Console.Write("String Not Accepted"); else { if(n[0] == 'a') stateC(n.Substring(1)); else if (n[0] == 'b') stateI(n.Substring(1)); }} // State Cstatic void stateC(string n){ if(n.Length == 0) Console.Write("String Not Accepted"); else { if(n[0] == 'a') stateD(n.Substring(1)); else if (n[0] == 'b') stateJ(n.Substring(1)); }} // State Dstatic void stateD(string n){ if(n.Length == 0) Console.Write("String Not Accepted"); else { if(n[0] == 'a') stateQ2(n); else if (n[0] == 'b') stateE(n.Substring(1)); }} // State Estatic void stateE(string n){ if(n.Length == 0) Console.Write("String Not Accepted"); else { if(n[0] == 'a') stateQ2(n); else if (n[0] == 'b') stateF(n.Substring(1)); }} // State Fstatic void stateF(string n){ if(n.Length == 0) Console.Write("String Not Accepted"); else { if(n[0] == 'a') stateQ2(n.Substring(1)); else if (n[0] == 'b') stateG(n.Substring(1)); }} // State G static void stateG(string n){ if(n.Length == 0) Console.Write("String Accepted"); else { if(n[0] == 'a') stateQ2(n); else if (n[0] == 'b') stateQ2(n); }} // State Hstatic void stateH(string n){ if(n.Length == 0) Console.Write("String Not Accepted"); else { if(n[0] == 'a') stateI(n.Substring(1)); else if (n[0] == 'b') stateK(n.Substring(1)); }} // State I static void stateI(string n){ if(n.Length == 0) Console.Write("String Not Accepted"); else { if(n[0] == 'a') stateJ(n.Substring(1)); else if (n[0] == 'b') stateL(n.Substring(1)); }} // State Jstatic void stateJ(string n){ if(n.Length == 0) Console.Write("String Not Accepted"); else { if(n[0] == 'a') stateE(n.Substring(1)); else if (n[0] == 'b') stateM(n.Substring(1)); }} // State Kstatic void stateK(string n){ if(n.Length == 0) Console.Write("String Not Accepted"); else { if(n[0] == 'a') stateL(n.Substring(1)); else if (n[0] == 'b') stateN(n.Substring(1)); }} // State Lstatic void stateL(string n){ if(n.Length == 0) Console.Write("String Not Accepted"); else { if(n[0] == 'a') stateM(n.Substring(1)); else if (n[0] == 'b') stateO(n.Substring(1)); }}// State Mstatic void stateM(string n){ if(n.Length == 0) Console.Write("String Not Accepted"); else{ if(n[0] == 'a') stateF(n.Substring(1)); else if (n[0] == 'b') stateP(n.Substring(1)); }}// State Nstatic void stateN(string n){ if(n.Length == 0) Console.Write("String Not Accepted"); else if(n[0] =='a') stateO(n.Substring(1)); else if (n[0] == 'b') stateQ1(n);}// State Qstatic void stateO(string n){ if(n.Length == 0) Console.Write("String Not Accepted"); else { if(n[0] == 'a') stateP(n.Substring(1)); else if(n[0] == 'b') stateQ1(n); }} // State Pstatic void stateP(string n){ if(n.Length == 0) Console.Write("String Not Accepted"); else { if(n[0] == 'a') stateG(n.Substring(1)); else if (n[0] == 'b') stateQ1(n.Substring(1)); }} // State Q1static void stateQ1(string n){ Console.Write("String Not Accepted");} // State Q2static void stateQ2(string n){ Console.Write("String Not Accepted");} // Driver codepublic static void Main (string[] args){ // Take string input string n = "abaabb"; // Call stateA // to check the input stateA(n);} }// This code is contributed by rutvik_56 |
String 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.

