Given a number S in the form of string, the task is to check whether the sum of digits at odd places is equal to the sum of digits at even places or not.
Example 1:
Input: S = "132"
Output: 1
Explanation: The sum of digits at odd 
places is 1+2=3. Similarly the sum of 
digits at even places is 3. Since they 
are equal,the answer is 1.
Example 2:
Input: S = "123"
Output: 0
Explanation: The sum of digits at odd 
places is 1+3=4. The sum of digits at 
even places is 2. Since,the sums are 
not equal,Thus answer is 0.
Your Task:
You don't need to read input or print anything.Your task is to complete the function oddAndEven() which takes an integer S (in the form of string) as input parameter and returns 1 if the sum of digits at odd places is equal to the sum of digits at even places.Otherwise, it returns 0.
Expected Time Complexity:O(|S|)
Expected Auxillary Space:O(1)
Constraints:
1≤ |S| ≤ 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: Mathematics

Starter code: #User function Template for python3



class Solution:

    def oddAndEven(self,S):

        odd,even=0,0

        for i in range(len(S)):

            if i%2:

                even+=int(S[i])

            else:

                odd+=int(S[i])

        return 1 if even==odd else 0  

Solutions:
class Solution:

	def oddAndEven(self, S):
		(odd, even) = (0, 0)
		for i in range(len(S)):
			if i % 2:
				even += int(S[i])
			else:
				odd += int(S[i])
		return 1 if even == odd else 0


class Solution:

	def oddAndEven(self, S):
		e = 0
		o = 0
		for i in range(len(S)):
			if i % 2 == 0:
				e = e + int(S[i])
			else:
				o = o + int(S[i])
		if e == o:
			return 1
		else:
			return 0


class Solution:

	def oddAndEven(self, S):
		sum1 = 0
		sum2 = 0
		for i in range(len(S)):
			if i == 0 or i % 2 == 0:
				sum1 = sum1 + int(S[i])
			else:
				sum2 = sum2 + int(S[i])
		if sum1 == sum2:
			return 1
		else:
			return 0


class Solution:

	def oddAndEven(self, S):
		(odd, even) = (0, 0)
		for i in range(len(S)):
			if i % 2:
				even += int(S[i])
			else:
				odd += int(S[i])
		return 1 if even == odd else 0
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		S = input()
		ob = Solution()
		print(ob.oddAndEven(S))


class Solution:

	def oddAndEven(self, S):
		l = list(S)
		l1 = []
		l2 = []
		for i in range(len(l)):
			if i % 2 == 0:
				l1.append(l[i])
			else:
				l2.append(l[i])
		s = 0
		s1 = 0
		for i in l1:
			s += int(i)
		for i in l2:
			s1 += int(i)
		if s == s1:
			return 1
		else:
			return 0


class Solution:

	def oddAndEven(self, S):
		x = 0
		y = 0
		for i in range(len(S)):
			if i % 2:
				x += int(S[i])
			else:
				y += int(S[i])
		return 1 if x == y else 0


class Solution:

	def oddAndEven(self, S):
		es = 0
		os = 0
		for i in range(1, len(S) + 1):
			if i % 2 != 0:
				es += int(S[i - 1])
			else:
				os += int(S[i - 1])
		if es == os:
			return 1
		return 0