Ishaan has bought a new gun. He is practicing his shooting, but he has to reload his gun again and again.
You are given his shooting a bullet and reloading activity in the form of a string s.
String s consists of 2 type of distinct characters: 'S' stands for shooting one bullet whereas 'R' stands for reloading the gun.
You are also given an integer n, which is the bullet capacity of the gun. Each time Ishaan reloads the gun, it is filled up to n bullets.
Given the string s and integer n, find out whether the activities are valid or not.
Assume that the gun is loaded upto its full capacity in the beginning of each test case.
Example 1:
Input:
s = "SSSRSRSS", n = 3
Output: 1
Explanation: He has 3 bullets at the start,
fires 3, reloades 3 bullets again, fires 1,
reloades 3 and then fires 2.
Ã¢â¬â¹Example 2:
Input: 
s = "SSSSR", n = 3
Output: 0
Explanation: He has 3 bullets at the start
and tries to fire 4 without reloading. 
Hence, not a valid activity.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isValid() which takes the string s and the integer n as inputs and returns true if the string denotes a valid activity. Else, it returns false.
Expected Time Complexity: O(|s|).
Expected Auxiliary Space: O(1).
Constraints:
1<=|s|<=10^{5}
1<=n<=10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3



class Solution:

    def isValid(self, s, n):

        # code here

Solutions:
class Solution:

	def isValid(self, s, n):
		if n == 0:
			return
		k = n
		i = 0
		while i < len(s):
			if s[i] == 'R':
				k = n
			else:
				k -= 1
				if k < 0:
					return 0
			i += 1
		return 1


class Solution:

	def isValid(self, s, n):
		count = n
		for item in s:
			if item == 'R':
				count = n
			else:
				count -= 1
				if count == -1:
					return 0
		return 1


class Solution:

	def isValid(self, s, n):
		n1 = n
		for i in s:
			if i == 'S':
				n1 -= 1
				if n1 < 0:
					return 0
			if i == 'R':
				n1 = n
		return 1


class Solution:

	def isValid(self, s, n):
		arr = s.split('R')
		ans = []
		for e in arr:
			ans.append(len(e) <= n)
		if ans.__contains__(False):
			return 0
		else:
			return 1


class Solution:

	def isValid(self, s, n):
		if len(max(s.split('R'))) <= n:
			return 1
		else:
			return 0


class Solution:

	def isValid(self, s, n):
		c = n
		for i in s:
			if i == 'R':
				c = n
			else:
				c -= 1
			if c < 0:
				return 0
		return 1


class Solution:

	def isValid(self, s, n):
		count_S = n
		for i in s:
			if i == 'S':
				count_S -= 1
			elif i == 'R':
				count_S = n
			if count_S < 0:
				return 0
		return 1


class Solution:

	def isValid(self, s, n):
		count = 0
		for i in range(len(s)):
			if count > n:
				return 0
			if s[i] == 'S':
				count += 1
			if s[i] == 'R':
				count = 0
		return 1


class Solution:

	def isValid(self, s, n):
		bullet = n
		for i in s:
			if i == 'S':
				if bullet <= 0:
					return 0
				else:
					bullet = bullet - 1
			if i == 'R':
				bullet = n
		return 1


class Solution:

	def isValid(self, s, n):
		x = n
		u = []
		f = 0
		for i in range(len(s)):
			if s[i] == 'S':
				x = x - 1
				u.append(x)
			elif s[i] == 'R':
				x = n
				u.append(x)
		for i in range(len(u)):
			if u[i] < 0:
				f += 1
		if f > 0:
			return 0
		else:
			return 1


class Solution:

	def isValid(self, s, n):
		b = 1
		for i in s:
			if i == 'S' and b <= n:
				b += 1
			elif i == 'R':
				b = 1
			else:
				return 0
		return 1


class Solution:

	def isValid(self, s, n):
		b = 0
		c = 0
		for i in s:
			if i == 'S':
				b += 1
				if b > n:
					return 0
			else:
				c = 1
				b = 0
		else:
			return 1


class Solution:

	def isValid(self, s, n):
		count = 0
		for i in s:
			if count > n:
				return 0
			if i == 'S':
				count += 1
			else:
				count = 0
		return 1