Given a string S contains 0's, 1's, and 2's, the task is to find the number of goals on the penalty.
	 '1' stands for "goal".
	 '0' stands for "no goal".
	 '2' stands for a foul which gives a penalty.
Example 1:
Input: S = "1012012112110"
Output: 2
Explanation: There are 3 penalties,
of which he scores only 2.
1012012112110
Example 2:
Input: S = "111122"
Output: 0
Explanation: No goal on penalty
Your Task:
You don't need to read input or print anything. Your task is to complete the function penaltyScore() which takes a string S as input and returns the goals count on the penalty. 
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(1).
Constraints:
1 <= |N| <= 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3

class Solution:

	def penaltyScore(self, S):

		# code here

Solutions:
class Solution:

	def penaltyScore(self, S):
		goal = 0
		for i in range(len(S)):
			if i < len(S) - 1:
				if S[i] == '2' and S[i + 1] == '1':
					goal = goal + 1
		return goal


class Solution:

	def penaltyScore(self, S):
		d = 0
		k = list(map(int, str(S)))
		for i in range(0, len(S) - 1):
			if k[i] == 2 and k[i + 1] == 1:
				d = d + 1
		return d


class Solution:

	def penaltyScore(self, s):
		c = 0
		for i in range(len(s) - 1):
			if s[i] == '2' and s[i + 1] == '1':
				c = c + int(s[i + 1])
		return c


class Solution:

	def penaltyScore(self, S):
		c = 0
		for i in range(len(S) - 1):
			if S[i:i + 2] == '21':
				c = c + 1
		return c


class Solution:

	def penaltyScore(self, S):
		l = []
		a = []
		for i in S:
			l.append(int(i))
		for i in range(len(l)):
			if l[i] == 2:
				if i + 1 < len(l):
					a.append(l[i + 1])
		count = 0
		for i in a:
			if i == 1:
				count += 1
		return count


class Solution:

	def penaltyScore(self, S):
		return S.count('21')


class Solution:

	def penaltyScore(self, S):
		count = 0
		for i in range(len(S) - 1):
			if S[i] == '2':
				if S[i + 1] == '1':
					count += 1
		return count


class Solution:

	def penaltyScore(self, S):
		l = list()
		for i in S:
			l.append(int(i))
		p = 0
		for i in range(1, len(l)):
			if l[i] == 1:
				if l[i - 1] == 2:
					p = p + 1
		return p


class Solution:

	def penaltyScore(self, S):
		res = 0
		for i in range(len(S) - 1):
			if S[i] + S[i + 1] == '21':
				res += 1
		return res


class Solution:

	def penaltyScore(self, S):
		if '21' in S:
			output = S.count('21')
			return output
		else:
			return 0


from collections import Counter

class Solution:

	def penaltyScore(self, S):
		z = S.count('21')
		if z == 0:
			return 0
		return z


class Solution:

	def penaltyScore(self, s):
		ans = s.count('21')
		return ans


class Solution:

	def penaltyScore(self, S):
		count = 0
		i = 0
		while i < len(S):
			while i < len(S) - 1 and S[i] == '2':
				if S[i + 1] == '1':
					count += 1
					i += 1
				else:
					i += 1
			i += 1
		return count


class Solution:

	def penaltyScore(self, S):
		stack_top = ''
		goals = 0
		for i in S:
			if stack_top == '2' and i == '1':
				goals += 1
			stack_top = i
		return goals


class Solution:

	def penaltyScore(self, S):
		c = 0
		k = 0
		for i in range(len(S) - 1):
			if S[i] == '2' and S[i + 1] == '1':
				c += 1
				k = 1
		if k == 0:
			return 0
		else:
			return c


class Solution:

	def penaltyScore(self, S):
		x = S.count('21')
		if x == 0:
			return 0
		return x


class Solution:

	def penaltyScore(self, S):
		n = '21'
		t = S.count(n)
		if t == 0:
			return 0
		return t


class Solution:

	def penaltyScore(self, S):
		s = '21'
		count = 0
		for i in range(1, len(S)):
			if S[i] == '1' and S[i - 1] == '2':
				count += 1
			else:
				continue
		return count if count > 0 else 0


class Solution:

	def penaltyScore(self, S):
		c = 0
		for x in range(len(S)):
			if S[x] == '2' and x + 1 < len(S):
				if S[x + 1] == '1':
					c += 1
		return c


class Solution:

	def penaltyScore(self, S):
		result = 0
		temp = ''
		count = 0
		for i in S:
			if i == '2':
				temp = '2'
			elif i == '1':
				temp += i
				if temp == '21':
					count += 1
					temp = ''
			else:
				temp = ''
		return count


class Solution:

	def penaltyScore(self, S):
		n = len(S)
		count = 0
		for i in range(len(S)):
			if i == n - 1:
				continue
			elif S[i] == '2' and S[i + 1] == '1':
				count += 1
		return count


class Solution:

	def penaltyScore(self, S):
		lst = list(S)
		s = 0
		for i in range(len(lst)):
			if i + 1 < len(lst):
				if lst[i] == '2':
					if lst[i + 1] == '1':
						s += 1
		return s