Prerequisites: Finite Automata
Given a string str consisting of characters a, b and c, check if the number of occurances of any character in the string is a multiple of 3 or not.
Input: str = bc
Explanation: The string consists 0 a’s and 3 * 0 = 0.
Input: str = abccc
Explanation: The string consists 3 c’s.
Input: str = abc
Output: NOT ACCEPTED
An NFA or a Nondeterministic Finite Automata is very similar to a DFA. It is a finite state machine which accepts a string(under some specific condition) if it reaches a final state, otherwise rejects it. The additional features which an NFA has are:
- Null move is allowed i.e., it can move forward without reading symbols.
- Ability to transmit to any number of states for a particular input.
NFA Machine that accepts all strings in which the occurrences of atleast one character is a multiple of 3:
For the above problem statement, we must first build an NFA machine. NFA machine is similar to a flowchart with various states and transitions. NFA machine corresponding to the above problem is shown below, Q3, Q4 and Q8 are the final states:
How does this NFA Machine work:
The working of the machine depends on checking if the string has 3 multiples of a’s or b’s or c’s.
- Case 1: Number of a’s is a multiple of three:
- To check whether the number of a’s in the string is a three multiple or not, a separate set of states is defined. The states defined as Q2, Q3, Q4 check whether the number of a’s is a multiple of three or not. If at any point this case reaches the final state Q2, then the number of a’s is a multiple of three.
- Case 2: Number of b’s is a multiple of three:
- To check whether the number of b’s in the string is a three multiple or not, a separate set of states is defined. The states defined as Q5, Q6, Q7 check whether the number of b’s is a multiple of three or not. If at any point this case reaches the final state Q5, then the number of b’s is a multiple of three.
- Case 3: Number of c’s is a multiple of three:
- To check whether the number of c’s in the string is a three multiple or not, a separate set of states is defined. The states defined as Q8, Q9, Q10 check whether the number of c’s is a multiple of three or not. If at any point this case reaches the final state Q8, then the number of c’s is a multiple of three.
If any of the above mentioned states reach the final states Q2, Q5 or Q8; then the string satisfies the condition
Below is the implementation of the above approach:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Print the most occurring character in an array of strings
- Program to build a DFA to accept strings that start and end with same character
- Minimum K such that every substring of length atleast K contains a character c
- Maximum occurring character in a linked list
- Maximum occurring character in an input string | Set-2
- Count substrings with each character occurring at most k times
- Return maximum occurring character in an input string
- Check if max occurring character of one string appears same no. of times in other
- Program to accept a Strings which contains all the Vowels
- Program to accept Strings starting with a Vowel
- Build a DFA to accept Binary strings that starts or ends with "01"
- Count of strings possible by replacing two consecutive same character with new character
- Rearrange the given string such that all prime multiple indexes have same character
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Python | Replacing Nth occurrence of multiple characters in a String with the given character
- Sub-strings that start and end with one character and have at least one other
- Character pairs from two strings with even sum
- Sub-strings of length K containing same character
- Count of sub-strings that contain character X at least once
- Check if there is any common character in two given strings
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.