**You are given a string S of length N containing only characters a,b,c
In one move you can modify S as follow
- Choose three indices ????,????,k(1≤i<????<????≤∣????∣) such that ????????=a ,????????=b,????????=c.
That is, choose some subsequence of ???? that equals abc. - Then, delete either the a or the c from ???? that is, either index ???? or k.
Note that this reduces the length of ???? by 1.
Find the minimum number of moves that can be made on S, such that it’s impossible to perform any further moves on the resulting string.
Ex: S = “abcc”
Choose ????=1,????=2,????=3 (Consider 1-indexing) delete index 1 Now, ???? = bcc and no more moves can be performed.**
I tried to solve the question come up with some logic but it doesn’t pass the hidden test case. I would like the share my code below
My Code:
for _ in range(int(input())): #This is for getting number of test cases
n = int(input())
s = input()
a = 0
b = 0
c = 0
d = {}
d["a"] = 0
for i in range(n):
if(s[i] == "a"):
a+=1
elif(s[i] == "b" and a>0):
b+=1
d["a"]+=a
a = 0
elif(s[i] == "c" and (d["a"]>0 and b>0)):
c+=1
print(min(d["a"],c))
But I saw some other some people submission and after looking that I corrected some lines of code. The code was successfully passed all the testcases. Since testcases are hidden in the codechef I can’t see what’s wrong with my logic.
Corrected Code:
for _ in range(int(input())):
n = int(input())
s = input()
a = 0
b = 0
c = 0
d = {}
d["a"] = 0
ans = 0
for i in range(n):
if(s[i] == "a"):
a+=1
elif(s[i] == "b" and a>0):
b+=1
d["a"]+=a
a = 0
elif(s[i] == "c" and (d["a"]>0 and b>0)):
d["a"]-=1
ans+=1
print(ans)
I would like someone to give me a testcase that fails in my code and runs correctly in corrected code.
Roshan Vijey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.