You are given a number. Your task is to check if there exists a permutation of the digits of this number which is divisible by 4. ^{ }
 
Example 1:
Input: 003 
Output: 1
Explanation: For 003, we have a permutation
300  which is  divisible by 4.
Example 2:
Input:
123456
Output:
1
Explanation:
For 123456, we have 123564 which is a
permutation of 123456 and is divisible by 4.
 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function divisible_by_four() which takes string as input parameter and returns 1 if any permutaion of string is divisible by 4 otherwise returns 0.
 
Expected Time Complexity: O(N^2)
Expected Auxiliary Space: O(1)
 
Constraints:
1<=|S|<=200

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Mathematics

Starter code: #User function Template for python3


class Solution:
    def divisible_by_four(self, s):
        # Code here

Solutions:
class Solution:

	def divisible_by_four(self, s):
		if s == '4' or s == '8':
			return 1
		odd = 0
		even = 0
		four = 0
		for item in s:
			item = int(item)
			odd += item % 2
			even += item % 2 == 0
			four += item % 4 == 0
		return int(even > four and odd > 0 or (four and even > 1))


class Solution:

	def divisible_by_four(self, s):
		if s == '4' or s == '0' or s == '8':
			return 1
		for i in range(len(s)):
			for j in range(i + 1, len(s)):
				if int(s[i] + s[j]) % 4 == 0 or int(s[j] + s[i]) % 4 == 0:
					return 1
		return 0


class Solution:

	def divisible_by_four(self, s):
		if int(s) < 10:
			return int(not int(s) % 4)
		s = str(s)
		l = len(s)
		for x in range(l - 1):
			for y in range(x + 1, l):
				t = s[x] + s[y]
				if int(t) % 4 == 0 or int(t[::-1]) % 4 == 0:
					return 1
		return 0


class Solution:

	def divisible_by_four(self, s):
		n = len(s)
		if n == 1:
			if int(s[0]) % 4 == 0:
				return 1
		for i in range(n):
			if s[i] == '0':
				continue
			for j in range(n):
				if i != j:
					if int(s[i] + s[j]) % 4 == 0:
						return 1
		return 0