VOOZH about

URL: https://www.geeksforgeeks.org/dsa/dfa-that-begins-with-a-but-does-not-contain-substring-aab/

⇱ DFA that begins with 'a' but does not contain substring 'aab' - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

DFA that begins with 'a' but does not contain substring 'aab'

Last Updated : 15 Jul, 2025

Prerequisite: Introduction to Deterministic Finite Automata 
Construct a DFA that accepts string str starting with input alphabet 'a' but does not contain 'aab' as a substring over input {a, b}.

Examples: 

Input: str = "babba" 
Output: Not Accepted 
Explanation: 
The given string doesn't start with 'a'.

Input: str = "abbaaaaa" 
Output: Accepted 
Explanation: 
The given string start with 'a'and doesn't contains "aab" as a substring. 
 

Approach: 
The transition table helps to understand how the transition of each state takes place on the input alphabets. In the transition table initial state is represented by ---> and the final state is represented by *. There are 3 final states, one initial and one dead state. 

State Transition table of the given DFA: 
 

STATEINPUT (a)INPUT (b)
---> AB*Q (dead state)
B*C*D*
C*C*Q (dead state)
D*B*D*
Q (dead state)Q (dead state)Q (dead State)


Below is the DFA diagram: 

👁 Image

Below is the implementation of the above DFA: 


Output: 
Not Accepted

 

Time Complexity: O(N)  
Auxiliary Space: O(N)

Comment
Article Tags:
Article Tags: