VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-build-dfa-starts-end-input-b/

⇱ Program to build DFA that starts and end with 'a' from input (a, b) - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program to build DFA that starts and end with 'a' from input (a, b)

Last Updated : 18 Apr, 2023

DFA (Deterministic Finite Automaton or Acceptor) is a finite state machine that accepts or rejects strings of symbols. DFA accepts the string if it reaches the final state and rejects otherwise. 

Now the problem is, provided a string as input character by character and we have to check whether the string starts and ends with 'a'. We can only store the current character, since there is no concept of memory and hence the DFA cannot store the string provided. Otherwise, we could have just checked the first and last character for this problem. The input set for this problem is (a, b).

We cannot store anything accept the current character, which make this program a little different and tough than other string related problems. 

Examples: 

Input : a b a b a
Output : Yes
Explanation : (a b a b a) starts and 
end with 'a'

Input : a b a b b
Output : No
Explanation : (a b a b b) starts with 
'a' but doesn't end with 'a' 

We first build a DFA for this problem. Making DFA is like making a flowchart for this program and then implement it in any language. You should have the knowledge of DFA and Finite Automata.

The DFA for given problem is: 

👁 Image

Implementation:


Output
a b a b a b a b b b a b a 
YES

Time Complexity: O(MAX) 

Auxiliary Space: O(1)

Comment
Article Tags: