Program to build a DFA that accepts strings starting and ending with different character
Last Updated :
14 Sep, 2022
Prerequisite: Deterministic Finite Automata
Given a string, str consists of characters ‘a’ & ‘b’. The task is to check whether string str starts and ends with different characters or not. If it does, print ‘YES’ with state transitions, else print ‘NO’.
Examples:
Input: ababab
Output: YES
Explanation:
The string “ababab” is starting with ‘a’ and ends with ‘b’
Input : ababa
Output : NO
Explanation:
The string “ababab” is starting with ‘a’ and ends with ‘a’
In DFA, there is no concept of memory, therefore we have to check the string character by character, beginning with the 0th character. The input set of characters for the problem is {a, b}. For a DFA to be valid, there must a transition rule defined for each symbol of the input set at every state to a valid state.
DFA Machine: For the above problem statement build a DFA machine. It is similar to a flowchart with various states and transitions. DFA machine corresponding to the above problem is shown below, Q2 and Q4 are the final states:
Explanation:
Suppose the first character in the input string is ‘a’, then on reading ‘a’, the control will shift to the upper branch of the machine. Now, it is defined that the string must not end with an ‘a’ to be accepted. At state Q1, if again ‘a’ comes, it keeps circling at the same state because for the machine the last read character might be the last character of the string. If it gets a ‘b’, then it can go to the final state, since a string ending in ‘b’ is acceptable in this case, so it moves to state Q2. Here, if it gets an ‘a’, it again enters the non-final state else for consecutive ‘b’s, it keeps circling in the final state.
The same is in the case when the first character is detected as ‘b’.
Approach:
- Define the minimum number of states required to make the state diagram. Use functions to various states.
- List all the valid transitions. Each state must have a transition for every valid symbol.
- Define the final states by applying the base condition.
- Define all the state transitions using state function calls.
- Define a returning condition for the end of the string.
For the given DFA Machine, the specifications are as follows:
- Q0, Q1, Q2, Q3, Q4 are the defined states.
- a and b are valid symbols. Each state has a transition defined for a and b.
- Q2 and Q4 are defined as the final state. If the string input ends at any of these states, it is accepted else rejected.
- Suppose at state Q0, if ‘a’ comes, the function call is made to Q1. If ‘b’ comes, the function call is made to Q3.
- If by following the process, the program reaches the end of the string, the output is made according to the state the program is at.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool q1(string, int );
bool q2(string, int );
bool q3(string, int );
bool q4(string, int );
vector<string> state_transition;
bool q1(string s, int i)
{
state_transition.push_back( "q1" );
if (i == s.length()) {
return false ;
}
if (s[i] == 'a' )
return q1(s, i + 1);
else
return q2(s, i + 1);
}
bool q2(string s, int i)
{
state_transition.push_back( "q2" );
if (i == s.length()) {
return true ;
}
if (s[i] == 'a' )
return q1(s, i + 1);
else
return q2(s, i + 1);
}
bool q3(string s, int i)
{
state_transition.push_back( "q3" );
if (i == s.length()) {
return false ;
}
if (s[i] == 'a' )
return q4(s, i + 1);
else
return q3(s, i + 1);
}
bool q4(string s, int i)
{
state_transition.push_back( "q4" );
if (i == s.length()) {
return true ;
}
if (s[i] == 'a' )
return q4(s, i + 1);
else
return q3(s, i + 1);
}
bool q0(string s, int i)
{
state_transition.push_back( "q0" );
if (i == s.length()) {
return false ;
}
if (s[i] == 'a' )
return q1(s, i + 1);
else
return q3(s, i + 1);
}
int main()
{
string s = "ababab" ;
bool ans = q0(s, 0);
if (ans) {
cout << "YES" << endl;
for ( auto & it : state_transition) {
cout << it << ' ' ;
}
}
else
cout << "NO" << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static Vector state_transition = new Vector();
static boolean q1(String s, int i)
{
state_transition.add( "q1" );
if (i == s.length())
{
return false ;
}
if (s.charAt(i) == 'a' )
return q1(s, i + 1 );
else
return q2(s, i + 1 );
}
static boolean q2(String s, int i)
{
state_transition.add( "q2" );
if (i == s.length())
{
return true ;
}
if (s.charAt(i) == 'a' )
return q1(s, i + 1 );
else
return q2(s, i + 1 );
}
static boolean q3(String s, int i)
{
state_transition.add( "q3" );
if (i == s.length())
{
return false ;
}
if (s.charAt(i) == 'a' )
return q4(s, i + 1 );
else
return q3(s, i + 1 );
}
static boolean q4(String s, int i)
{
state_transition.add( "q4" );
if (i == s.length())
{
return true ;
}
if (s.charAt(i) == 'a' )
return q4(s, i + 1 );
else
return q3(s, i + 1 );
}
static boolean q0(String s, int i)
{
state_transition.add( "q0" );
if (i == s.length())
{
return false ;
}
if (s.charAt(i) == 'a' )
return q1(s, i + 1 );
else
return q3(s, i + 1 );
}
public static void main (String[] args)
{
String s = "ababab" ;
boolean ans = q0(s, 0 );
if (ans == true )
{
System.out.println( "YES" );
for ( int index = 0 ; index < state_transition.size(); index++)
{
System.out.print((String)state_transition.get(index) + ' ' );
}
}
else
System.out.println( "NO" );
}
}
|
Python3
state_transition = []
def q1(s, i):
state_transition.append( "q1" )
if (i = = len (s)):
return False
if (s[i] = = 'a' ):
return q1(s, i + 1 )
else :
return q2(s, i + 1 )
def q2(s, i):
state_transition.append( "q2" )
if (i = = len (s)):
return True
if (s[i] = = 'a' ):
return q1(s, i + 1 )
else :
return q2(s, i + 1 )
def q3(s, i):
state_transition.append( "q3" )
if (i = = len (s)):
return False
if (s[i] = = 'a' ):
return q4(s, i + 1 )
else :
return q3(s, i + 1 )
def q4(s, i):
state_transition.append( "q4" )
if (i = = len (s)):
return True
if (s[i] = = 'a' ):
return q4(s, i + 1 )
else :
return q3(s, i + 1 )
def q0(s, i):
state_transition.append( "q0" )
if (i = = len (s)):
return False
if (s[i] = = 'a' ):
return q1(s, i + 1 )
else :
return q3(s, i + 1 )
s = "ababab"
ans = q0(s, 0 )
if (ans):
print ( "YES" )
for it in state_transition:
print (it, end = " " )
else :
print ( "NO" )
|
C#
using System;
using System.Collections;
class GFG{
static ArrayList state_transition =
new ArrayList();
static bool q1( string s, int i)
{
state_transition.Add( "q1" );
if (i == s.Length)
{
return false ;
}
if (s[i] == 'a' )
return q1(s, i + 1);
else
return q2(s, i + 1);
}
static bool q2( string s, int i)
{
state_transition.Add( "q2" );
if (i == s.Length)
{
return true ;
}
if (s[i] == 'a' )
return q1(s, i + 1);
else
return q2(s, i + 1);
}
static bool q3( string s, int i)
{
state_transition.Add( "q3" );
if (i == s.Length)
{
return false ;
}
if (s[i] == 'a' )
return q4(s, i + 1);
else
return q3(s, i + 1);
}
static bool q4( string s, int i)
{
state_transition.Add( "q4" );
if (i == s.Length)
{
return true ;
}
if (s[i] == 'a' )
return q4(s, i + 1);
else
return q3(s, i + 1);
}
static bool q0( string s, int i)
{
state_transition.Add( "q0" );
if (i == s.Length)
{
return false ;
}
if (s[i] == 'a' )
return q1(s, i + 1);
else
return q3(s, i + 1);
}
public static void Main ( string [] args)
{
string s = "ababab" ;
bool ans = q0(s, 0);
if (ans == true )
{
Console.Write( "YES\n" );
for ( int index = 0;
index < state_transition.Count;
index++)
{
Console.Write(
( string )state_transition[index] + ' ' );
}
}
else
Console.Write( "NO" );
}
}
|
Javascript
<script>
var state_transition = [];
function q1(s, i) {
state_transition.push( "q1" );
if (i === s.length) {
return false ;
}
if (s[i] === "a" ) return q1(s, i + 1);
else return q2(s, i + 1);
}
function q2(s, i) {
state_transition.push( "q2" );
if (i === s.length) {
return true ;
}
if (s[i] === "a" ) return q1(s, i + 1);
else return q2(s, i + 1);
}
function q3(s, i) {
state_transition.push( "q3" );
if (i === s.length) {
return false ;
}
if (s[i] === "a" ) return q4(s, i + 1);
else return q3(s, i + 1);
}
function q4(s, i) {
state_transition.push( "q4" );
if (i === s.length) {
return true ;
}
if (s[i] === "a" ) return q4(s, i + 1);
else return q3(s, i + 1);
}
function q0(s, i) {
state_transition.push( "q0" );
if (i === s.length) {
return false ;
}
if (s[i] === "a" ) return q1(s, i + 1);
else return q3(s, i + 1);
}
var s = "ababab" ;
var ans = q0(s, 0);
if (ans === true ) {
document.write( "YES <br>" );
for ( var index = 0; index < state_transition.length; index++) {
document.write(state_transition[index] + " " );
}
} else document.write( "NO" );
</script>
|
Output:
YES
q0 q1 q2 q1 q2 q1 q2
Time Complexity: O(n) where a string of length n requires traversal through n states.
Auxiliary Space: O(n), for storing the states in the array.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...