- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if binary string multiple of 3 using DFA in Python
Suppose we have an array n that represents a binary representation of any number. We have to check whether its binary representation is divisible by three or not by using Deterministic Finite Automata DFA.
So, if the input is like n = [1, 1, 0, 0] (binary of 12), then the output will be True.
To solve this, we can construct DFA like below −
The approach is simple when a number is divisible by 3 then the remainder will be 0, if not then remainder will be 1 or 2. There are three states for these three remainders. The initial state is also final state because when remainder is 0 it means the number is divisible.
To solve this, we will follow these steps −
- dfa_state := 0
- for i in range 0 to size of nums - 1, do
- digit := nums[i]
- if dfa_state is 0, then
- if digit is same as 1, then
- dfa_state := 1
- if digit is same as 1, then
- otherwise when dfa_state is 1, then
- if digit is same as 0, then
- dfa_state := 2
- otherwise,
- dfa_state := 0
- if digit is same as 0, then
- otherwise when dfa_state is 2, then
- if digit is same as 0, then
- dfa_state := 1
- if digit is same as 0, then
- if dfa_state is 0, then
- return True
- return False
Let us see the following implementation to get better understanding −
Example
def solve(nums): dfa_state = 0 for i in range(len(nums)): digit = nums[i] if dfa_state == 0: if digit == 1: dfa_state = 1 elif dfa_state == 1: if digit == 0: dfa_state = 2 else: dfa_state = 0 elif dfa_state == 2: if digit == 0: dfa_state = 1 if dfa_state == 0: return True return False n = [1, 1, 0, 0] print(solve(n))
Input
[1, 1, 0, 0]
Output
True
- Related Articles
- Python - Check if a given string is binary string or not
- Check if an encoding represents a unique binary string in Python
- How to check if multiple strings exist in another string in Python?
- Program to check if binary string has at most one segment of ones or not using Python
- Check if all the 1s in a binary string are equidistant or not in Python
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string in Python
- Check if binary representation of a number is palindrome in Python
- Check if binary representations of two numbers are anagram in Python
- Check if a binary string contains all permutations of length k in C++
- Check If a String Contains All Binary Codes of Size K in C++
- How can we check if specific string occurs multiple times in another string in Java?
- Check if a string can be formed from another string using given constraints in Python
- Check if a binary string has two consecutive occurrences of one everywhere in C++
- Check if leaf traversal of two Binary Trees is same in Python
- Check if a binary string contains consecutive same or not in C++

Advertisements