![]() |
VOOZH | about |
Given a string S, the task is to design a for accepting the language L = {aNbM | N ≥ 0, M ≥ 0, N+M ≥ 1}. , i.e., a regular language L such that all 'a' occur before the first occurrence of 'b' {a, ab, aab, bb..., }. If the given string follows the given language L, then print “Accepted”. Otherwise, print “Not Accepted”.
Examples
Input: S = "aabbb"
Output: Accepted
Explanation: All the 'a' come before 'b' s.Input: S = "ba"
Output: Not Accepted
Explanation: 'b' comes before 'a'.Input: S = "aaa"
Output: Accepted
Explanation: Note that 'b' does not need to occur in SInput: S = "b"
Output: Accepted
Explanation: Note that 'a' does not need to occur in S
Approach: The problem can be accepted only when the following cases are met:
This can be better visualized with the help of the state transition diagram of the DFA
State Transition Diagram:
Below is the implementation of the above approach:
NOT ACCEPTED
Time Complexity: O(N) where N is the length of the string
Auxiliary Space: O(1)