Problem:


Score : 300 points

Problem Statement

We have a string X, which has an even number of characters. Half the characters are S, and the other half are T.

Takahashi, who hates the string ST, will perform the following operation 10^{10000} times:

Among the occurrences of ST in X as (contiguous) substrings, remove the leftmost one. If there is no occurrence, do nothing.

Find the eventual length of X.

Constraints

2 ≦ |X| ≦ 200,000

The length of X is even.

Half the characters in X are S, and the other half are T.

Partial Scores

In test cases worth 200 points, |X| ≦ 200.

Input

The input is given from Standard Input in the following format:

X

Output

Print the eventual length of X.

Sample Input 1

TSTTSS

Sample Output 1

4

In the 1-st operation, the 2-nd and 3-rd characters of TSTTSS are removed.
X becomes TTSS, and since it does not contain ST anymore, nothing is done in the remaining 10^{10000}-1 operations.
Thus, the answer is 4.

Sample Input 2

SSTTST

Sample Output 2

0

X will eventually become an empty string: SSTTST ⇒ STST ⇒ ST ⇒ ``.

Sample Input 3

TSSTTTSS

Sample Output 3

4

X will become: TSSTTTSS ⇒ TSTTSS ⇒ TTSS.



Sample input:
TSTTSS


Sample output:
4

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect
import copy
import math
from itertools import accumulate
sys.setrecursionlimit(10**6)

# lis_of_lis = [[] for _ in range(N)]


def zz(): return list(map(int, sys.stdin.readline().split()))


def z(): return int(sys.stdin.readline())


def S(): return sys.stdin.readline()[:-1]


def C(line): return [sys.stdin.readline() for _ in range(line)]


X = S()
num_S = [0] * len(X)
s_count = 0
t_count = 0
for x in reversed(X):
    if (x == 'S'):
        s_count += 1
    else:
        break
        t_count += 1
print(s_count*2)


# for i, x in enumerate(X):
#     if(x == 'S'):
#         num_S[i] = 1

# num_S = list(accumulate(num_S))
# print(num_S)

Submission status: Wrong Answer

Solution code:
s=input() ; n=len(s)
for i in range(100):
    s=s.replace("ST","")
print(len(s))

Submission status: Wrong Answer

Solution code:
def main():
    #input data
    import sys
    input = lambda:sys.stdin.readline().strip()
    S = input()
    
    #solve
    from itertools import groupby
    def runLengthEncode(S: str)->list:
        '''
        str to list(tuple).required itertools.groupby
        '''
        grouped = groupby(S)
        res = []
        for k, v in grouped:
            res.append((k, len(list(v))))
        return res

    S = runLengthEncode(S)
    ans = 0
    idx=0
    cnt=10**1000
    flag=True
    while idx<len(S):
        if idx<len(S)-1 and flag:
            if S[idx][0]=='S' and S[idx+1][0]=='T':
                
                t=min(S[idx][1],S[idx+1][1])

                if cnt-t<0:
                    ans+=S[idx][1]+S[idx+1][1]-(cnt-t)*2
                    flag=False
                
                else:
                    ans+=abs(S[idx][1]-S[idx+1][1])
                idx+=2
                continue
        ans+=S[idx][1]
        idx+=1
    print(ans)

if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    #input data
    import sys
    input = lambda:sys.stdin.readline().strip()
    S = input()
    
    #solve
    from itertools import groupby
    def runLengthEncode(S: str)->list:
        '''
        str to list(tuple).required itertools.groupby
        '''
        grouped = groupby(S)
        res = []
        for k, v in grouped:
            res.append((k, len(list(v))))
        return res

    S = runLengthEncode(S)
    ans = 0
    idx=0
    cnt=0

    while idx<len(S) and cnt<=10**1000:
        if idx<len(S)-1:
            if S[idx][0]=='S' and S[idx+1][0]=='T':
                tmp=abs(S[idx][1]-S[idx+1][1])
                cnt+=min(S[idx][1],S[idx+1][1])*2
                ans+=tmp
                idx+=2
                continue
        ans+=S[idx][1]
        idx+=1
    print(ans)

if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    #input data
    import sys
    input = lambda:sys.stdin.readline().strip()
    S = input()
    
    #solve
    from itertools import groupby
    def runLengthEncode(S: str)->list:
        '''
        str to list(tuple).required itertools.groupby
        '''
        grouped = groupby(S)
        res = []
        for k, v in grouped:
            res.append((k, len(list(v))))
        return res

    S = runLengthEncode(S)
    ans = 0
    idx=0
    cnt=0

    while idx<len(S) and cnt<=10**1000:
        if idx<len(S)-1:
            if S[idx][0]=='S' and S[idx+1][0]=='T':
                tmp=abs(S[idx][1]-S[idx+1][1])
                cnt+=min(S[idx][1],S[idx+1][1])
                ans+=tmp
                idx+=2
                continue
        ans+=S[idx][1]
        idx+=1
    print(ans)

if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    #input data
    import sys
    input = lambda:sys.stdin.readline().strip()
    S = input()
    
    #solve
    from itertools import groupby
    def runLengthEncode(S: str)->list:
        '''
        str to list(tuple).required itertools.groupby
        '''
        grouped = groupby(S)
        res = []
        for k, v in grouped:
            res.append((k, len(list(v))))
        return res

    S = runLengthEncode(S)
    ans = 0

    idx=0
    while idx<len(S):
        if idx<len(S)-1:
            if S[idx][0]=='S' and S[idx+1][0]=='T':
                ans+=abs(S[idx][1]-S[idx+1][1])
                idx+=2
                continue
        ans+=S[idx][1]
        idx+=1
    print(ans)

if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    #input data
    import sys
    input = lambda:sys.stdin.readline().strip()
    S = input()
    
    #solve
    from itertools import groupby
    def runLengthEncode(S: str)->list:
        '''
        str to list(tuple).required itertools.groupby
        '''
        grouped = groupby(S)
        res = []
        for k, v in grouped:
            res.append((k, len(list(v))))
        return res

    S = runLengthEncode(S)
    ans = 0

    while S:
        x = S.pop(0)
        if S:
            if x[0]=='S' and S[0][0]=='T':
                y = S.pop(0)
                ans += abs(x[1]-y[1])
            else:
                ans += x[1]
        else:
            ans += x[1]
    print(ans)


if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
x = list(input())[::-1]
ans = 0
for i in range(len(x)):
    if x[i] == "S":
        ans += 1
    else:
        break
print(ans*2)

Submission status: Wrong Answer

Solution code:
S = input()
N = len(S)
t_cnt = 0
s_cnt = 0

for i in range(N):
    if S[i] == 'T':
        t_cnt += 1
    else:
        break

for i in range(N):
    if S[N-1-i] == 'S':
        s_cnt += 1
    else:
        break

print(max(t_cnt,s_cnt) * 2)

Submission status: Wrong Answer

Solution code:
def cal():
    x = input()
    i = 0
    while i <= 10**3:
        i += 1
        x = x.replace('ST', '', 10**5)
    print(len(x))

cal()

Submission status: Wrong Answer

Solution code:
def cal():
    x = input()
    i = 0
    while i <= 2*10**5:
        i += 1
        x1 = x.replace('ST', '', 10**5)
    print(len(x))

cal()

Submission status: Wrong Answer

Solution code:
x = input()
ans, count0, count1 = len(x), 0, 0
for i in x:
    if i == 'S' and count1 != 0:
        ans -= 2*min(count0, count1)
        count0, count1 = 1, 0
    elif i == 'S':
        count0 += 1
    else:
        count1 += 1
ans -= 2*min(count0, count1)
print(ans)

Submission status: Wrong Answer

Solution code:
x = input()
ans, count0, count1 = len(x), 0, 0
for i in x:
    if i == 'S' and count1 == 0:
        count0 += 1
    elif i == 'T' and count0 > 0:
        count1 += 1
    else:
       ans -= 2*min(count0, count1)
       if i == 'S':
           count0, count1 = 1, 0
       else:
           count0, count1 = 0, 0
ans -= 2*min(count0, count1)
print(ans)

Submission status: Wrong Answer

Solution code:
x = input()
ans, count0, count1 = len(x), 0, 0
for i in x:
    if i == 'S' and count1 == 0:
        count0 += 1
    elif i == 'T':
        count1 += 1
    else:
       ans -= 2*min(count0, count1)
       if i == 'S':
           count0, count1 = 1, 0
       else:
           count0, count1 = 0, 0
ans -= 2*min(count0, count1)
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

s = input()
q = deque()

for si in s:
    print(q)
    if si == "S":
        q.append(si)
    elif si == "T" and len(q) == 0:
        q.append(si)
    elif q[-1] == "T":
        q.append(si)
    elif q[-1] == "S":
        q.pop()
print(len(q))

Submission status: Wrong Answer

Solution code:
s = input()

sc = 0
tc = 0
ans = len(s)
for si in s:
    if si == "S":
        sc += 1
    else:
        tc += 1
        if sc:
            ans -= min(sc,tc)*2
            sc -= min(sc,tc)
            tc -= min(sc,tc)
        #print(ans,sc,tc)
print(ans)

Submission status: Wrong Answer

Solution code:
X = input()

for i in range(len(X)):
  if X[i] == "S":
    ans = i * 2
    break

for i in range(len(X)):
  if X[-1 -i] == "T":
    ans = max(ans, i * 2)
    break

print(ans)

Submission status: Wrong Answer

Solution code:
X = input().rstrip() + "#"
s0 = X[0]
P = []
cnt = 1
for s in X[1:]:
    if s==s0:
        cnt += 1
    else:
        P.append(cnt)
        cnt = 1
    s0 = s
res = 0
if X[0] == "T":
    res = P[0]*2
if X[-2] == "S":
    res = max(P[-1]*2, res)
print(res)

Submission status: Wrong Answer

Solution code:
X = input().rstrip() + "#"
s0 = X[0]
P = []
cnt = 1
for s in X[1:]:
    if s==s0:
        cnt += 1
    else:
        P.append(cnt)
        cnt = 1
    s0 = s
if X[0] == "T":
    P = P[1:]
res = len(X) - 1
for i in range(0,len(P)-1,2):
    if P[i] < P[i+1]:
        res -= P[i]*2
    else:
        res -= P[i+1]*2
print(res)

Submission status: Wrong Answer

Solution code:
s = input()
try:
    rt = s.rindex("T")
except ValueError:
    rt = -1
try:
    ls = s.index("S")
except ValueError:
    ls = -1
if rt == -1:
    st, sc = 0, 0
elif ls == -1:
    st, sc = 0, 0
else:
    st = s[ls : rt + 1].count("T")
    sc = s[ls : rt + 1].count("S")
ans = len(s) - min(st, sc) * 2

print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
rt = s.rindex("T")
ls = s.index("S")
st = s[ls : rt + 1].count("T")
sc = s[ls : rt + 1].count("S")
ans = len(s) - min(st, sc) * 2

print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
rt = s.rindex("T")

st = s[: rt + 1].count("T")
sc = s[: rt + 1].count("S")
ans = len(s) - min(st, sc) * 2

print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[12]:


X = input()


# In[15]:


mylist = X
while "ST" in mylist:
    if mylist == "ST":
        mylist = ""
    else:
        idx = X.index("ST")
        mylist = mylist[:idx] + mylist[idx+2:]
print(len(mylist))


# In[ ]:

Submission status: Wrong Answer

Solution code:
X = str(input())

flag = True
s = 0
for i in range(len(X)):
  if flag:
    if X[i] == 'S':
      s = i
      break
      
flag = True
t = 0
for i in range(len(X)):
  if flag:
    if X[len(X)-i-1] == 'T':
      t = len(X)-i
      break
      
n = min(X[s:t].count('S'), X[s:t].count('T'))
ans = len(X) - 2*n
print(ans)

Submission status: Wrong Answer

Solution code:
X = input()

s, t = 0, len(X)//2
cnt = 0

for x in X:
    if x == 'S':
        s += 1
        if t > 0:
            cnt += 1
    else:
        t -= 1

print(len(X)-cnt*2)

Submission status: Wrong Answer

Solution code:
from collections import deque
x = input()
stack = deque(x[0])
for i in range(1,len(x)):
    if x[i] == "S":
        stack.appendleft(x[i])
    else: #x[i] == "T"
        if len(stack) == 0:
            stack.append(x[i])
        if stack[0] == "T":
            stack.appendleft(x[i])
        else:
            stack.popleft()
print(len(stack))

Submission status: Wrong Answer

Solution code:
from collections import deque

X = deque(list(input()))
ans = len(X)

#頭のTを読み捨てる
while 1:
    #print(X)
    if X[0] == "T":
        X.popleft()
    else:
        X.popleft()
        S_cnt = 1
        T_cnt = 0
        mode = "S"
        break
        
    if len(X) == 0:
        break

#Sが出てきて以後の処理
if mode == "S":
    while 1:
        #print(X,T_cnt,S_cnt)
        if len(X) == 0:
            ans -= 2*min(S_cnt,T_cnt)
            break
        elif mode == "S":
            if X[0] == "T":
                X.popleft()
                mode = "T"
                T_cnt = 1
            else:
                X.popleft()
                S_cnt += 1
        elif mode == "T":
            if X[0] == "T":
                X.popleft()
                T_cnt += 1
            else:
                ans -= 2*min(S_cnt,T_cnt)
                X.popleft()
                mode = "S"
                T_cnt = 0
                S_cnt = 1
        
print(ans)

Submission status: Wrong Answer

Solution code:
X = input()
N = len(X)
start = X.index("S")
end = N - X[::-1].index("T")
cnt_S = 0
cnt_T = 0
for x in X[start:end]:
    if x == "S":
        cnt_S += 1
    else:
        cnt_T += 1
print(start + N - end + abs(cnt_S - cnt_T))

Submission status: Wrong Answer

Solution code:
x = input()
print(len(x) - 2*x.count("ST"))

Submission status: Wrong Answer

Solution code:
s=input()
ans=0
a=0
b=0
for i in range(len(s)):
    if s[i]=="T":
        a+=1
    else:
        break
for i in range(len(s)):
    if s[-i-1]=="S":
        b+=1
    else:
        break
print(a+b)

Submission status: Wrong Answer

Solution code:
s=input()
ans=len(s)
i=0
c=-1
tmp1=0#Sが連続した数
tmp2=0
while i<len(s):
    if c==1:
        if s[i]=="S":
            tmp=1
            c=0
            ans-=2*min(tmp1,tmp2)
            tmp1=1
            tmp2=0
        else:
            tmp2+=1
            if i==len(s)-1:
                ans-=2*min(tmp1,tmp2)
            
    elif c==0:
        if s[i]=="S":
            tmp1+=1
        else:
            c=1
            tmp2=1
            if i==len(s)-1:
                ans-=2*min(tmp1,tmp2)
    elif c==-1:
        if s[i]=="S":
            c=0
            tmp1=1
        else:
            c=1
            tmp2=1
    i+=1
    #print(ans,tmp1,tmp2)
print(ans)

Submission status: Wrong Answer

Solution code:
x = input()
print((n := len(x)) - x[:n // 2].count("S") * 2)

Submission status: Wrong Answer

Solution code:
x = input()
print(len(x) - min(x[:x.rfind("T")].count("S"), x[x.find("S"):].count("T")) * 2)

Submission status: Wrong Answer

Solution code:
from collections import deque

X = input()
stack = deque([X[0]])
before = X[0]
for i in range(1, len(X)):
    if X[i] == 'S':
        stack.appendleft(X[i])

    if X[i] == 'T':
        if stack:
            check = stack.popleft()
        if check == 'T':
            stack.appendleft(check)
            stack.appendleft(X[i])

print(len(stack))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-


from itertools import groupby


def runLengthEncode(S: str):
    grouped = groupby(S)
    res = []
    for k, v in grouped:
        res.append((k, str(len(list(v)))))
    return res


def main():
    s = input()
    length = len(s)
    s_count = 0
    t_count = 0

    # See:
    # https://qiita.com/Kept1994/items/e9179d1dd7c6455d6883
    runLength = runLengthEncode(s)

    for first, second in zip(runLength, runLength[1:]):
        if first[0] == 'S' and second[0] == 'T':
            s_count += int(first[1])
            t_count += int(second[1])

    print(length - 2 * min(s_count, t_count))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

X = list(input())
length = len(X)

cur = 0
cnt = 0
for i in range(length):
    cur = max(cur, i+1)
    x = X[i]
    if x=='S':
        while cur<length:
            v = X[cur]
            if v=='T':
                cnt += 2
                break
            cur += 1
print(length-cnt)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-


from itertools import groupby


def runLengthEncode(S: str):
    grouped = groupby(S)
    res = []
    for k, v in grouped:
        res.append((k, str(len(list(v)))))
    return res


def main():
    s = input()
    count = len(s)

    # See:
    # https://qiita.com/Kept1994/items/e9179d1dd7c6455d6883
    runLength = runLengthEncode(s)

    for first, second in zip(runLength, runLength[1:]):
        if first[0] == 'S' and second[0] == 'T':
            first_count = int(first[1])
            second_count = int(second[1])
            count -= min(first_count, second_count) * 2

    print(count)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque
S=list(map(lambda x: 1 if x=="T" else 0,list("ST"*10**4)))

q=deque()

for i in S:

    if i:
        if (not q) or q[-1]!=0:
            q.append(i)
        elif q and q[-1]==0:
            q.pop()
    else:
        q.append(i)
print(len(q))

Submission status: Wrong Answer

Solution code:
X = input().strip()
N = len(X)
inds = -1
indt = N
for i in range(N):
    if X[i]=="S":
        inds = i
        break
for i in range(N-1,-1,-1):
    if X[i]=="T":
        indt = i
        break
print(2*max(inds,N-1-indt))

Submission status: Wrong Answer

Solution code:
X = list(input())
N = len(X)
LT,RS = 0,0
while X[LT]=="T":
    LT+=1
while X[N-1-RS]=="S":
    RS+=1
print(max(LT,RS)*2)

Submission status: Wrong Answer

Solution code:
x=list(input())
num=[]
s="S"
cnt=len(x)
for i in range(len(x)):
    if x[i]==s:
        cnt=i
        break
cnt2=0
for i in range(cnt,len(x)):
    if x[i]!=s:
        if s=="T":
            s="S"
        else:
            s="T"
        num.append(cnt2)
        cnt2=1
    else:
        cnt2+=1
num.append(cnt2)
ans=0
num3=0
if len(num)%2==1:
    num3=num[-1]
for i in range(len(num)//2):
    if num[-2*(i+1)]>=num[-2*i-1]+num3:
        ans+=(num[-2*i-1]+num3)*2
        num3=0
    else:
        ans+=(num[-2*(i+1)])*2
        num3=num[-2*i-1]+num3-num[-2*(i+1)]
print(len(x)-ans)

Submission status: Wrong Answer

Solution code:
x=list(input())
num=[]
s="S"
cnt=len(x)
for i in range(len(x)):
    if x[i]==s:
        cnt=i
        break
cnt2=0
for i in range(cnt,len(x)):
    if x[i]!=s:
        if s=="T":
            s="S"
        else:
            s="T"
        num.append(cnt2)
        cnt2=1
    else:
        cnt2+=1
num.append(cnt2)
ans=0
num3=0
if len(num)%2==1:
    del(num[-1])
for i in range(len(num)//2):
    if num[-2*(i+1)]>=num[(-2*i)-1]+num3:
        ans+=num[(-2*i)+1]+num3
        num3=0
    else:
        ans+=num[-2*(i+1)]
        num3=num[(-2*i)-1]+num3-num[-2*(i+1)]
print(len(x)-(ans*2))

Submission status: Wrong Answer

Solution code:
x=list(input())
num=[]
s="S"
cnt=len(x)
for i in range(len(x)):
    if x[i]==s:
        cnt=i
        break
cnt2=0
for i in range(cnt,len(x)):
    if x[i]!=s:
        if s=="T":
            s="S"
        else:
            s="T"
        num.append(cnt2)
        cnt2=1
    else:
        cnt2+=1
num.append(cnt2)
ans=0
num3=0
if len(num)%2==1:
    num3=num[-1]
for i in range(len(num)//2):
    if num[-2*(i+1)]>=num[-2*i-1]+num3:
        ans+=(num[-2*i+1]+num3)*2
        num3=0
    else:
        ans+=(num[-2*(i+1)])*2
        num3=num[-2*i-1]+num3-num[-2*(i+1)]
print(len(x)-ans)

Submission status: Wrong Answer

Solution code:
x=list(input())
num=[]
s="S"
cnt=len(x)
for i in range(len(x)):
    if x[i]==s:
        cnt=i
        break
cnt2=0
for i in range(cnt,len(x)):
    if x[i]!=s:
        if s=="T":
            s="S"
        else:
            s="T"
        num.append(cnt2)
        cnt2=1
    else:
        cnt2+=1
num.append(cnt2)
ans=0
num3=0
if len(num)%2==1:
    del(num[-1])
for i in range(len(num)//2):
    if num[-2*(i+1)]>=num[-2*i-1]+num3:
        ans+=(num[-2*i+1]+num3)*2
        num3=0
    else:
        ans+=(num[-2*(i+1)])*2
        num3=num[-2*i-1]+num3-num[-2*(i+1)]
print(len(x)-ans)

Submission status: Wrong Answer

Solution code:
x=list(input())
t=x.count("T")
s=x.count("S")
num=0
num2=0
if x[0]=="T":
    num=1
    for i in range(1,len(x)):
        if x[i]=="S":
            break
        else:
            num+=1
if x[-1]=="S":
    num2=1
    for i in range(len(x)-1):
        if x[-2-i]=="T":
            break
        else:
            num2+=1
t-=num
s-=num2
print((s+t)-(min(s,t)*2)+num+num2)

Submission status: Wrong Answer

Solution code:
X = input()

num = X.find('S')
inv_num = X.rfind('T')

X_num = len(X)

S_num = X[num:inv_num+1].count('S')
T_num = X[num:inv_num+1].count('T')

print(X_num- min(S_num,T_num)*2)

Submission status: Wrong Answer

Solution code:
s = input().rstrip()
n = len(s)

removed = 0
cnt_s = cnt_t = 0

for i in range(n):
    if s[i] == 'S':
        cnt_s += 1
    else:
        cnt_t += 1
    if cnt_t > cnt_s or i == n-1:
        removed += cnt_s
        cnt_s = cnt_t = 0
#     print(cnt_s,cnt_t)
removed += min(cnt_s,cnt_t)
print(len(s) - removed*2)

Submission status: Wrong Answer

Solution code:
s = input().rstrip()

ans = 0
cnt_s = cnt_t = 0

for c in s:
    if c == 'S':
        cnt_s += 1
    else:
        cnt_t += 1
    if cnt_t > cnt_s:
        ans += cnt_s
        cnt_s = cnt_t = 0
ans += cnt_s
print(len(s) - ans*2)

Submission status: Wrong Answer

Solution code:
S = input()
n = len(S) // 2
c = 0
for s in S:
  if s == 'T':
    c += 1
  else:
    break
d = 0
for s in S[::-1]:
  if s == 'S':
    d += 1
  else:
    break
print(c+d + abs(c-d))

Submission status: Wrong Answer

Solution code:
S = input()
a = S.count('S')
b = S.count('T')
c = 0
for s in S:
  if s == 'T':
    c += 1
  else:
    break
d = 0
for s in S[::-1]:
  if s == 'S':
    d += 1
  else:
    break
print(c + d + abs(a-d - (b-c)))

Submission status: Wrong Answer

Solution code:
S = input()
a = S.count('S')
b = S.count('T')
c = 0
for s in S:
  if s == 'T':
    c += 1
  else:
    break
d = 0
for s in S[::-1]:
  if s == 'S':
    d += 1
  else:
    break
c + d + abs(a-d - (b-c))

Submission status: Wrong Answer

Solution code:
w = input()

mode = 1
c_s = 0
ans = 0

for i in w:
#    print(i)
    if i == 'S':
        if mode == 0:
            c_s += 1
#            print(ans,mode,c_s)
#            print('a')
        elif mode == 1:
            c_s = 1
            mode = 0
#            print(ans,mode,c_s)
#            print('b')
    else:
        if mode == 0:
            ans += 1
            mode = 1
            c_s -= 1
#            print(ans,mode,c_s)
#            print('c')
        elif mode == 1 and c_s > 0:
            ans += 1
            c_s -= 1
#            print(ans,mode,c_s)
#            print('d')

print(len(w)-(2*ans))

Submission status: Wrong Answer

Solution code:
def main():
    x = input().replace("ST", "S T").split()
    ans = 0
    tmp = 0
    for i in range(len(x)-1):
        s = x[i].count("S")
        t = x[i+1].count("T")
        ans += max(t-s, 0) + len(x[i]) - s - tmp
        tmp = t- max(t-s, 0)
        if i == len(x)-2:
            ans += len(x[i+1]) - t
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    x = input().replace("TS", "T S").split()
    ans = 0
    s = 0
    for i in x:
        s += i.count("S")
        t = i.count("T")
        if t <= s:
            s -= t
            ans += s
        else:
            ans += 2*(t-s)
            s = 0
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import collections

def main():
    x = input().replace("TS", "T S").split()
    ans = 0
    for i in x:
        s = i.count("S")
        t = i.count("T")
        ans += abs(s-t)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
s = input()
s_cnt, t_cnt = 0, 0
n = len(s)
for i in range(n):
    if s[i] == "S":
        for j in range(i, n):
            if s[j] == "T":
                t_cnt += 1
        
        break

for i in range(n - 1, -1, -1):
    if s[i] == "T":
        for j in range(i, -1, -1):
            if s[j] == "S":
                s_cnt += 1
        break
print(n - min(t_cnt, s_cnt) * 2)

Submission status: Wrong Answer

Solution code:
s = input()

s_cnt, t_cnt = 0, 0
ans = 0
for i in range(len(s)):
    if s[i] == "S":
        s_cnt += 1
    else:
        t_cnt += 1
        if i == len(s) - 1 or s[i + 1] == "S":
            ans += min(t_cnt, s_cnt)
            t_cnt = 0
            s_cnt = max(0, s_cnt - t_cnt)

print(len(s) - ans * 2)

Submission status: Wrong Answer

Solution code:
s = input()

s_cnt, t_cnt = 0, 0
ans = 0
for i in range(len(s)):
    if s[i] == "S":
        s_cnt += 1
    else:
        t_cnt += 1
        if i == len(s) - 1 or s[i + 1] == "S":
            ans += min(t_cnt, s_cnt)
            t_cnt = 0
            c_cnt = 0

print(len(s) - ans * 2)

Submission status: Wrong Answer

Solution code:
x = input()
n = len(x)

checkt = False
checks = False
s = 0
t = 0
for i in range(n-1,-1,-1):
    if(x[i] == 'S'):
        if(checkt):
            s += 1
    else:
        checkt = True

for i in range(n):
    if(x[i] == 'T'):
        if(checks):
            t += 1
    else:
        checks = True

print(n - min(s,t) * 2)

Submission status: Wrong Answer

Solution code:
def main():
    X = input()
    len_X = len(X)
    right_T = X.rfind("T")
    left_S = X.find("S")
    S_Count = X[left_S:right_T+1].count("S")
    T_Count = X[left_S:right_T+1].count("T")
    ans = len_X - min(T_Count, S_Count)*2
    print(ans)
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    X = input()
    len_X = len(X)
    right_T = X.rfind("T")
    S_Count = X[:right_T].count("S")
    ans = len_X - S_Count*2
    print(ans)
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque
X = input()
que = deque()
for x in X:
    if x=='S':
        que.append('S')
    elif x=='T' and (not que or que[-1]=='T'):
        que.append('T')
    elif x=='T' and que[-1]=='S':
        _ = que.pop()
    print(que)
print(len(que))

Submission status: Wrong Answer

Solution code:
from math import ceil,floor,comb,factorial,gcd,pow,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from copy import deepcopy
from time import time
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

x = input()
s = 0
t = 0
ans = len(x)
for i in range(len(x)):
    if x[i] == 'S':
        if t > 0:
            ans -= min(s, t)*2
            s = 0
            t = 0
        s += 1
    else:
        t += 1

ans -= min(s, t)*2
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import itertools

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_nl = lambda: list(map(int, readline().split()))
in_na = lambda: map(int, read().split())
in_s = lambda: readline().rstrip().decode('utf-8')


def main():
    S = list(in_s())[::-1]
    N = len(S)

    gr = itertools.groupby(S)

    count = 0
    tnum = 0
    for k, v in gr:
        num = sum(1 for _ in v)
        if k == 'S':
            count += min(tnum, num)
            tnum = 0
        else:
            tnum = num

    # print(count)

    ans = N - count * 2
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_nl = lambda: list(map(int, readline().split()))
in_na = lambda: map(int, read().split())
in_s = lambda: readline().rstrip().decode('utf-8')


def main():
    S = in_s()
    N = len(S)

    lt = 0
    rs = 0
    for i in range(N):
        if S[i] == 'T':
            lt += 1
        else:
            break
    for i in range(N - 1, -1, -1):
        if S[i] == 'S':
            rs += 1
        else:
            break

    snum = N // 2 - rs
    tnum = N // 2 - lt
    cnum = min(snum, tnum)
    ans = N - cnum * 2
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
  S = str(input())
  ans = 0
  num_s = 0
  flag = False
  for i in range(len(S)):
    if S[i] == 'S' and flag:
      num_s = 0
      flag = False
    if S[i] == 'S':
      num_s += 1
    if S[i] == 'T':
      flag = True
      if num_s > 0:
        ans += 2
        num_s-=1
  print(len(S)-ans)
  
if __name__ == "__main__":
  main()

Submission status: Wrong Answer

Solution code:
x=input()
a=[1]
for i in range(1,len(x)):
    if x[i]==x[i-1]:
        a[-1]+=1
    else:a+=[1]
ans=0    
if x[0]=="T":
    ans+=a.pop(0)
if x[-1]=="S":
    ans+=a.pop(-1)
    
while len(a)>1:
    a=[i-j for i,j in zip(a[::2],a[1::2]) if i!=j]
    while a and a[0]<0:
        ans+=abs(a.pop(0))
    while a and a[-1]>0:
        ans+=a.pop(-1)

print(ans+abs(a[0]) if a else ans)

Submission status: Wrong Answer

Solution code:
x=input()
a=[1]
for i in range(1,len(x)):
    if x[i]==x[i-1]:
        a[-1]+=1
    else:a+=[1]
ans=0    
if x[0]=="T":
    ans+=a.pop(0)
if x[-1]=="S":
    ans+=a.pop(-1)
    
while len(a)>1:
    a=[i-j for i,j in zip(a[::2],a[1::2]) if i!=j]
    if a:
        if a[0]<0:
            ans+=abs(a.pop(0))
    if a:
        if a[-1]>0:
            ans+=a.pop(-1)
    a=[abs(i) for i in a if i!=0]
print(ans+abs(a[0]) if a else ans)

Submission status: Wrong Answer

Solution code:
x=input()
a=[1]
for i in range(1,len(x)):
    if x[i]==x[i-1]:
        a[-1]+=1
    else:a+=[1]
ans=0    
if x[0]=="T":
    ans+=a.pop(0)
if x[-1]=="S":
    ans+=a.pop(-1)
    
while len(a)>1:
    a=[i-j for i,j in zip(a[::2],a[1::2])]
    a=[i for i in a if i!=0]
    if a:
        if a[0]<0:
            ans-=a.pop(0)
    if a:
        if a[-1]>0:
            ans+=a.pop(-1)
    a=[abs(i) for i in a if i!=0]
print(ans+abs(a[0]) if a else ans)

Submission status: Wrong Answer

Solution code:
x=input()
t=s=0
for i in range(len(x)):
    if x[i]=="T":
        t+=1
    else:break
x=x[::-1]
for i in range(len(x)):
    if x[i]=="S":
        s+=1
    else:break    
print(t+s+abs(x.count("T")-t-x.count("S")+s))

Submission status: Wrong Answer

Solution code:
x=input()
ans=0
for i in range(len(x)):
    if x[i]=="T":
        ans+=1
    else:break    
for j in range(-1,-len(x),-1):
    if x[j]=="S":
        ans+=1
    else:break    
x=x[i:j+1]
print(ans+abs(x.count("T")-x.count("S")))

Submission status: Wrong Answer

Solution code:
x = 'TSSTTTSS'
while 'ST' in x:
    x = x.replace('ST', '', 1)
print(len(x))

Submission status: Wrong Answer

Solution code:
x=input()
print((len(x)-x.rfind("T")-1)*2)

Submission status: Wrong Answer

Solution code:
x = input()

s, t = 0, 0
ols = 0
cnt = 0
for i in x:
  if i == "S":
    cnt += min(ols,t)
    t = 0
    s += 1
  else:
    if s > 0:
     ols = s
    t += 1
    s = 0
cnt += min(ols,t)
print (len(x)-cnt*2)

Submission status: Wrong Answer

Solution code:
x=input()
n=len(x)

t=0
cnt=0
for i in range(n-1,-1,-1):
    if x[i]=='T':
        t+=1
    elif t>0 and x[i]=='S':
        cnt+=1

print(n-2*cnt)