VOOZH about

URL: https://www.geeksforgeeks.org/projects/program-to-construct-dfa-for-regular-expression-c-a-b/

⇱ Program to construct DFA for Regular Expression C( A + B)+ - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program to construct DFA for Regular Expression C( A + B)+

Last Updated : 23 Jul, 2025

Given a string S, the task is to design a Deterministic Finite Automata (DFA) for accepting the language L = C (A + B)+. If the given string is accepted by DFA, then print "Yes". Otherwise, print "No".

Examples:

Input: S = "CABABABAB"
Output: Yes
Explanation: The given string is of the form C(A + B)+ as the first character is C and it is followed by A or B.

Input: S = "ABAB"
Output: No

Approach: The idea is to interpret the given language L = C (A + B)+ and for the regular expression of the form C(A + B)+, the following is the DFA State Transition Diagram:

👁 Image

Follow the steps below to solve the problem:

  • If the given string is of length less than equal to 1, then print "No".
  • If the first character is always C, then traverse the remaining string and check if any of the characters is A or B.
  • If there exists any character other than A or B while traversing in the above step, then print "No".
  • Otherwise, print "Yes".

Below is the implementation of the above approach:


Output: 
Yes

 

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

Comment