Program to construct DFA for Regular Expression C( A + B)+
Last Updated :
10 Feb, 2022
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:
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:
C++
#include <bits/stdc++.h>
using namespace std;
void DFA(string str, int N)
{
if (N <= 1) {
cout << "No" ;
return ;
}
int count = 0;
if (str[0] == 'C' ) {
count++;
for ( int i = 1; i < N; i++) {
if (str[i] == 'A' || str[i] == 'B' )
count++;
else
break ;
}
}
else {
cout << "No" ;
return ;
}
if (count == N)
cout << "Yes" ;
else
cout << "No" ;
}
int main()
{
string str = "CAABBAAB" ;
int N = str.size();
DFA(str, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void DFA(String str, int N)
{
if (N <= 1 )
{
System.out.print( "No" );
return ;
}
int count = 0 ;
if (str.charAt( 0 ) == 'C' )
{
count++;
for ( int i = 1 ; i < N; i++)
{
if (str.charAt(i) == 'A' ||
str.charAt(i) == 'B' )
count++;
else
break ;
}
}
else
{
System.out.print( "No" );
return ;
}
if (count == N)
System.out.print( "Yes" );
else
System.out.print( "No" );
}
public static void main(String[] args)
{
String str = "CAABBAAB" ;
int N = str.length();
DFA(str, N);
}
}
|
Python3
def DFA( str , N):
if (N < = 1 ):
print ( "No" )
return
count = 0
if ( str [ 0 ] = = 'C' ):
count + = 1
for i in range ( 1 , N):
if ( str [i] = = 'A' or str [i] = = 'B' ):
count + = 1
else :
break
else :
print ( "No" )
return
if (count = = N):
print ( "Yes" )
else :
print ( "No" )
if __name__ = = '__main__' :
str = "CAABBAAB"
N = len ( str )
DFA( str , N)
|
C#
using System;
class GFG
{
static void DFA( string str, int N)
{
if (N <= 1)
{
Console.Write( "No" );
return ;
}
int count = 0;
if (str[0] == 'C' ) {
count++;
for ( int i = 1; i < N; i++) {
if (str[i] == 'A'
|| str[i] == 'B' )
count++;
else
break ;
}
}
else {
Console.Write( "No" );
return ;
}
if (count == N)
Console.Write( "Yes" );
else
Console.Write( "No" );
}
static public void Main()
{
string str = "CAABBAAB" ;
int N = str.Length;
DFA(str, N);
}
}
|
Javascript
<script>
function DFA(str,N) {
if (N <= 1)
{
document.write( "No" );
return ;
}
let count = 0;
if (str[0] == 'C' )
{
count++;
for (let i = 1; i < N; i++)
{
if (str[i] == 'A' ||
str[i] == 'B' )
count++;
else
break ;
}
}
else
{
document.write( "No" );
return ;
}
if (count == N)
document.write( "Yes" );
else
document.write( "No" );
}
let str = "CAABBAAB" ;
let N = str.length;
DFA(str, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...