All right! Let's implement some pattern-matching using CPP strings.You are given a string s of x and y. You need to verify whether the string follows the pattern x^{n}y^{n}. That is the string is valid only if equal number of ys follow equal number of xs.
As an example: xxyyxxyy is valid. xy is valid. xxyyx is invalid. xxxyyyxxyyxy is valid.Example 1:Input:s = xxyyOutput:1Example 2:Input:s = xyxOutput:0Your Task:
Since this is a function problem, you don't need to take any input. Just complete the function follPatt(string s) that outputs 1 if string is valid, else it outputs 0. In a new line, print 1 if the string is valid else print 0.
Expected Time Complexity:O(|s|)
Expected Auxilary Space:O(1)Constraints:
1 <= |s| <=100

Source: geeksforgeeks | Difficulty: EASY

Starter code: #User function Template for python3

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

Solutions:
class Solution:

	def follPatt(self, s):
		l = []
		c = 1
		f = 1
		if s[0] == 'y':
			f = 0
		for i in range(len(s) - 1):
			if s[i] == s[i + 1]:
				c += 1
			else:
				l.append(c)
				c = 1
		l.append(c)
		if len(l) % 2 == 1:
			f = 0
		i = 0
		n = len(l)
		while i + 1 < n:
			if l[i] != l[i + 1]:
				f = 0
			i += 2
		print(f)


class Solution:

	def follPatt(self, s):
		c1 = 0
		c2 = 0
		n = 0
		if len(s) == 0:
			print(1)
			return
		if len(s) == 1 or s[0] == s[len(s) - 1] or s[0] == 'y':
			print(0)
			return
		for i in range(0, len(s) - 1):
			if s[i] == s[i + 1]:
				if s[i] == 'x':
					c1 += 1
				else:
					c2 += 1
			elif n % 2 != 0:
				n += 1
				if c1 != c2:
					print(0)
					return
			else:
				n += 1
		if c2 - c1 == 0:
			print(1)
		else:
			print(0)


class Solution:

	def follPatt(self, s):
		c1 = 0
		c2 = 0
		n = 0
		if len(s) == 0:
			print(1)
			return
		if len(s) == 1 or s[0] == s[len(s) - 1] or s[0] == 'y':
			print(0)
			return
		for i in range(0, len(s) - 1):
			if s[i] == s[i + 1]:
				if s[i] == 'x':
					c1 += 1
				else:
					c2 += 1
			elif n % 2 != 0:
				n += 1
				if c1 != c2:
					print(0)
					return
			else:
				n += 1
		if c2 - c1 == 0:
			print(1)
		else:
			print(0)
if __name__ == '__main__':
	T = int(input())
	for i in range(T):
		str = input().strip()
		obj = Solution()
		obj.follPatt(str)


class Solution:

	def follPatt(self, s):
		if s.count('x') != s.count('y'):
			print(0)
			return
		if s[-1] == 'x':
			print(0)
			return
		if s[len(s) // 2 - 1] == 'y':
			print(0)
			return
		print(1)


class Solution:

	def follPatt(self, s):
		flag = True
		count = 0
		i = 0
		while i < len(s):
			while i < len(s) and s[i] == 'x':
				count += 1
				i += 1
			while i < len(s) and s[i] == 'y':
				count -= 1
				i += 1
			if count != 0:
				flag = False
				print('0')
				break
		if flag and count == 0:
			print('1')