Program to build a DFA to accept strings that start and end with same character
Last Updated :
15 Dec, 2022
Given a string consisting of characters a and b, check if the string starts and ends with the same character or not. If it does, print ‘Yes’ else print ‘No’.
Examples:
Input: str = “abbaaba”
Output: Yes
Explanation:
The given input string starts and ends with same character ‘a’
So the states of the below DFA Machine will be q0->q1->q2->q2->q1->q1->q2->q1 and q1 is a final state,
Hence the output will be Yes.
Input: str = “ababab”
Output: No
Explanation:
The given input string starts and ends with different character ‘a’ and ‘b’,
So the states of the below DFA Machine will be q0->q1->q2->q1->q2->q1->q2 and q2 is not a final state,
Hence the output will be No.
Approach:
DFA or Deterministic Finite Automata is a finite state machine which accepts a string(under some specific condition) if it reaches a final state, otherwise rejects it.
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 that accepts all strings that start and end with same character
For the above problem statement, we must first build a DFA machine. DFA machine is similar to a flowchart with various states and transitions. DFA machine corresponding to the above problem is shown below, Q1 and Q3 are the final states:
How does this DFA Machine works:
The working of the machine depends on whether the first character is ‘a’ or ‘b’.
- Case 1: String starts with ‘a’
- 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 at the string must 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 has to leave the final state since a string ending in ‘b’ is not acceptable. So it moves to state Q2.
- Here, if it gets an ‘a’, it again enters the final state Q1 else for consecutive ‘b’s, it keeps circling.
- Case 2: String starts with ‘b’
- Suppose the first character in the input string is ‘b’, then on reading ‘b’, the control will shift to the upper branch of the machine.
- Now, it is defined at the string must end with an ‘b’ to be accepted.
- At state Q3, if again ‘b’ 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 ‘a’, then it has to leave the final state since a string ending in ‘a’ is not acceptable. So it moves to state Q4.
- Here, if it gets an ‘b’, it again enters the final state Q3 else for consecutive ‘a’s, it keeps circling.
Approach for designing the DFA machine:
- Define the minimum number of states required to make the state diagram. Here Q0, Q1, Q2, Q3, Q4 are the defined states. Use functions for various states.
- List all the valid transitions. Here ‘a’ and ‘b’ are valid symbols. Each state must have a transition for every valid symbol.
- Define the final states by applying the base condition. Q1 and Q3 are defined as the final state. If the string input ends at any of these states, it is accepted else rejected.
- Define all the state transitions using state function calls.
- Define a returning condition for the end of the string. 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;
void q1(string, int );
void q2(string, int );
void q3(string, int );
void q4(string, int );
void q1(string s, int i)
{
if (i == s.length()) {
cout << "Yes \n" ;
return ;
}
if (s[i] == 'a' )
q1(s, i + 1);
else
q2(s, i + 1);
}
void q2(string s, int i)
{
if (i == s.length()) {
cout << "No \n" ;
return ;
}
if (s[i] == 'a' )
q1(s, i + 1);
else
q2(s, i + 1);
}
void q3(string s, int i)
{
if (i == s.length()) {
cout << "Yes \n" ;
return ;
}
if (s[i] == 'a' )
q4(s, i + 1);
else
q3(s, i + 1);
}
void q4(string s, int i)
{
if (i == s.length()) {
cout << "No \n" ;
return ;
}
if (s[i] == 'a' )
q4(s, i + 1);
else
q3(s, i + 1);
}
void q0(string s, int i)
{
if (i == s.length()) {
cout << "No \n" ;
return ;
}
if (s[i] == 'a' )
q1(s, i + 1);
else
q3(s, i + 1);
}
int main()
{
string s = "abbaabb" ;
q0(s, 0);
}
|
Java
import java.io.*;
public class GFG
{
static void q1(String s, int i)
{
if (i == s.length())
{
System.out.println( "Yes" );
return ;
}
if (s.charAt(i) == 'a' )
q1(s, i + 1 );
else
q2(s, i + 1 );
}
static void q2(String s, int i)
{
if (i == s.length())
{
System.out.println( "No" );
return ;
}
if (s.charAt(i) == 'a' )
q1(s, i + 1 );
else
q2(s, i + 1 );
}
static void q3(String s, int i)
{
if (i == s.length())
{
System.out.println( "Yes" );
return ;
}
if (s.charAt(i) == 'a' )
q4(s, i + 1 );
else
q3(s, i + 1 );
}
static void q4(String s, int i)
{
if (i == s.length())
{
System.out.println( "No" );
return ;
}
if (s.charAt(i) == 'a' )
q4(s, i + 1 );
else
q3(s, i + 1 );
}
static void q0(String s, int i)
{
if (i == s.length())
{
System.out.println( "No" );
return ;
}
if (s.charAt(i) == 'a' )
q1(s, i + 1 );
else
q3(s, i + 1 );
}
public static void main (String[] args)
{
String s = "abbaabb" ;
q0(s, 0 );
}
}
|
Python3
def q1(s, i):
if (i = = len (s)):
print ( "Yes" );
return ;
if (s[i] = = 'a' ):
q1(s, i + 1 );
else :
q2(s, i + 1 );
def q2(s, i):
if (i = = len (s)):
print ( "No" );
return ;
if (s[i] = = 'a' ):
q1(s, i + 1 );
else :
q2(s, i + 1 );
def q3(s, i):
if (i = = len (s)):
print ( "Yes" );
return ;
if (s[i] = = 'a' ):
q4(s, i + 1 );
else :
q3(s, i + 1 );
def q4(s, i):
if (i = = s.length()):
print ( "No" );
return ;
if (s[i] = = 'a' ):
q4(s, i + 1 );
else :
q3(s, i + 1 );
def q0(s, i):
if (i = = len (s)):
print ( "No" );
return ;
if (s[i] = = 'a' ):
q1(s, i + 1 );
else :
q3(s, i + 1 );
if __name__ = = '__main__' :
s = "abbaabb" ;
q0(s, 0 );
|
C#
using System;
class GFG
{
static void q1( string s, int i)
{
if (i == s.Length)
{
Console.WriteLine( "Yes" );
return ;
}
if (s[i] == 'a' )
q1(s, i + 1);
else
q2(s, i + 1);
}
static void q2( string s, int i)
{
if (i == s.Length)
{
Console.WriteLine( "No" );
return ;
}
if (s[i] == 'a' )
q1(s, i + 1);
else
q2(s, i + 1);
}
static void q3( string s, int i)
{
if (i == s.Length)
{
Console.WriteLine( "Yes" );
return ;
}
if (s[i] == 'a' )
q4(s, i + 1);
else
q3(s, i + 1);
}
static void q4( string s, int i)
{
if (i == s.Length)
{
Console.WriteLine( "No" );
return ;
}
if (s[i] == 'a' )
q4(s, i + 1);
else
q3(s, i + 1);
}
static void q0( string s, int i)
{
if (i == s.Length)
{
Console.WriteLine( "No" );
return ;
}
if (s[i] == 'a' )
q1(s, i + 1);
else
q3(s, i + 1);
}
public static void Main ()
{
string s = "abbaabb" ;
q0(s, 0);
}
}
|
Javascript
<script>
function q1( s, i)
{
if (i == s.length) {
document.write( "Yes<br>" );
return ;
}
if (s[i] == 'a' )
q1(s, i + 1);
else
q2(s, i + 1);
}
function q2( s, i)
{
if (i == s.length) {
document.write( "No" );
return ;
}
if (s[i] == 'a' )
q1(s, i + 1);
else
q2(s, i + 1);
}
function q3( s, i)
{
if (i == s.length) {
document.write( "Yes" );
return ;
}
if (s[i] == 'a' )
q4(s, i + 1);
else
q3(s, i + 1);
}
function q4( s, i)
{
if (i == s.length) {
document.write( "No" );
return ;
}
if (s[i] == 'a' )
q4(s, i + 1);
else
q3(s, i + 1);
}
function q0( s, i)
{
if (i == s.length) {
document.write( "No" );
return ;
}
if (s[i] == 'a' )
q1(s, i + 1);
else
q3(s, i + 1);
}
var s = "abbaabb" ;
q0(s, 0);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...