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: Accepted

Solution code:
X = input()

res = 0
l = 0
for x in X:
    if x == 'S':
        l += 1
    elif l > 0:
        res += 1
        l -= 1
print(len(X)-2*res)

Submission status: Accepted

Solution code:
s = input()
stack, p = 0, 0
for i in s:
    if i == "S":
        stack += 1
    elif stack > 0 and i == "T":
        p += 1
        stack -= 1
    else:
        pass
print(len(s) - p * 2)

Submission status: Accepted

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

# In[18]:


from collections import deque


# In[12]:


X = input()


# In[26]:


mylist = deque([])
for x in X:
    if x == "S":
        mylist.append("S")
    else:
        if len(mylist) == 0 or mylist[-1] == "T":
            mylist.append("T")
        else:
            mylist.pop()
print(len(mylist))


# In[ ]:

Submission status: Accepted

Solution code:
X = str(input())

n = len(X)
tmp = 0
cnt = 0
for i in range(n):
  if X[n-1-i] == 'T':
    tmp += 1
  else:
    if tmp > 0:
      tmp -= 1
      cnt += 1

ans = n - 2*cnt
print(ans)

Submission status: Accepted

Solution code:
s = input()
ans = len(s)
temp1 = 0 #Sが連続
for i in range(len(s)):
  if s[i] == "S":
    temp1+=1
  elif s[i] == "T":
    if temp1 > 0:
      temp1-=1
      ans-=2
    else:
      temp1=0
  else:
    temp1=0
print(ans)

Submission status: Accepted

Solution code:
X = input()

s, t = 0, len(X)//2
cnt = 0
used = 0
for x in X:
    if x == 'S':
        s += 1
        if t-used > 0:
            cnt += 1
            used += 1
    else:
        used = max(0, used-1)
        t -= 1

print(len(X)-cnt*2)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)
# input = sys.stdin.readline    ####
def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')
# from math import ceil, floor, log2
from collections import deque, defaultdict
# from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product, permutations
# from heapq import heapify, heappop, heappush
# import numpy as np    # cumsum
# from bisect import bisect_left, bisect_right

def solve():
    X = input()
    # print(X)

    L = deque([])
    for now in X:
        if not L:
            L.append(now)
            continue

        if now == 'T':
            pre = L.pop()
            if pre == 'S':
                continue
            else:
                L.append(pre)
        L.append(now)

    print(len(L))


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)
# input = sys.stdin.readline    ####
def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')
# from math import ceil, floor, log2
from collections import deque, defaultdict
# from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product, permutations
# from heapq import heapify, heappop, heappush
# import numpy as np    # cumsum
# from bisect import bisect_left, bisect_right

def solve():
    X = input()
    # print(X)

    L = deque([])
    R = deque(list(X))
    # for _ in range(100):
    while R:
        if not L:
            L.append(R.popleft())
            continue

        pre = L.pop()
        now = R.popleft()
        if pre == 'S' and now == 'T':
            pass
        else:
            L.append(pre)
            L.append(now)
    print(len(L))


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
x=input()
ans=len(x)
p=0
for i in x:
    if i=="S":
        p+=1
    if p>0 and i=="T":
        p-=1
        ans-=2
print(ans)

Submission status: Accepted

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])
        elif stack[0] == "T":
            stack.appendleft(x[i])
        else:
            stack.popleft()
print(len(stack))

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil,pi,factorial
    from operator import itemgetter
    def I(): return int(input())
    def MI(): return map(int, input().split())
    def LI(): return list(map(int, input().split()))
    def LI2(): return [int(input()) for i in range(n)]
    def MXI(): return [[LI()]for i in range(n)]
    def SI(): return input().rstrip()
    def printns(x): print('\n'.join(x))
    def printni(x): print('\n'.join(list(map(str,x))))
    inf = 10**17
    mod = 10**9 + 7
#main code here!
    s=SI()
    n=len(s)
    ans=n
    pool=0
    for i in range(n):
        if s[i]=="T" and pool>0:
            pool-=1
            ans-=2
        if s[i]=="S":
            pool+=1
    print(ans)
            

                
    
    
    
            
if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
x = input()
n = len(x)
ans = []
for i in range(n):
    if x[i] == 'S':
        ans.append(x[i])
    else:
        if len(ans) > 0:
            if ans[-1] == 'S':
                ans.pop()
            else:
                ans.append(x[i])
        else:
            ans.append(x[i])
    # print(ans)

print(len(ans))

Submission status: Accepted

Solution code:
from collections import deque
right = deque(input())
left = deque()
while right:
    left.append(right.popleft())
    if not right:
        break
    while left and right and left[-1] == 'S' and right[0] == 'T':
        left.pop()
        right.popleft()
print(len(left))

Submission status: Accepted

Solution code:
s=list(input())[::-1]

s_cnt=0
t_cnt=0


for ss in s:
  if ss=="S":
    if t_cnt>0:
      t_cnt-=1
    else:
      s_cnt+=1
  else:
    t_cnt+=1
print(t_cnt+s_cnt)

Submission status: Accepted

Solution code:
def Solver(X):
    K=0
    P=0

    for s in X:
        if s=="S":
            P+=1
        elif P>0:
            P-=1
        else:
            K+=1

    return 2*K
#================================================
X=input()
print(Solver(X))

Submission status: Accepted

Solution code:
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(500000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

x=input()

scnt=0
m=0
for i in range(len(x))[::-1]:
    if x[i]=="T":
        m=i+1
        break

ans=len(x)
for i in range(m):
    if x[i]=="S":
        scnt+=1
    else:
        if scnt>0:
            scnt-=1
            ans-=2
    # print(scnt)
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.buffer.readline

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return input().rstrip().decode()
def II(): return int(input())
def FI(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode()

def main():
	x=RD()

	ans=0
	s=0

	for i in x:
		if i=="S":
			s+=1
		else:
			if s==0:
				ans+=1
			else:
				s-=1

	print(ans+s)









if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
X = input()
stack = []
for x in X:
    if stack and stack[-1] == "S" and x == "T":
        stack.pop()
    else:
        stack.append(x)
print(len(stack))

Submission status: Accepted

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate, product # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入g
import sys
sys.setrecursionlimit(10000000)
#mod = 10**9 + 7
#mod = 9982443453
mod = 998244353
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
s = input()
sc = 0
ans = 0
for i in range(len(s)):
    if s[i] == 'S':
        sc += 1
    else:
        if sc:
            sc -= 1
            ans += 1
print(len(s) - ans*2)

Submission status: Accepted

Solution code:
X = input().strip()
A = [X[0]]
for i in range(1,len(X)):
    if X[i]=="T" and len(A)>0 and A[-1]=="S":
        A.pop()
    else:
        A.append(X[i])
print(len(A))

Submission status: Accepted

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])

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

print(len(stack))

Submission status: Accepted

Solution code:
U = input()
S = [0]*len(U)
T = [0]*len(U)
for k in range(len(U)):
    if U[k] == "S":
        S[k] = 1
    else:
        T[k] = 1

for k in range(1,len(U)):
    S[k] += S[k-1]
    T[k] += T[k-1]
ans = 0
for k in range(len(U)):
    ans = max(ans,2*(T[k]-S[k]))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x = input().replace("ST", "")
s = 0
cnt = 0
for i in range(len(x)):
    if x[i] == "S": s += 1
    else:
        if s > 0: 
            s -= 1
            cnt += 1
print(len(x)-cnt*2)

Submission status: Accepted

Solution code:
l = 0
ans = 0
for c in input():
    if c == 'S':
        l += 1
    else:
        if l == 0:
            ans += 1
        else:
            l -= 1
print(ans + l)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

    # 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]) + s_count
            second_count = int(second[1])

            if first_count > second_count:
                s_count = first_count - second_count
            else:
                s_count = 0

            length -= 2 * min(first_count, second_count)

    print(length)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import bisect
from itertools import accumulate
X = input()
S = [0]*(len(X))
T = [0]*(len(X))
for i in range(len(X)):
    if X[i] == "S":
        S[i] = 1
    else:
        T[i]=1
Ssum = list(accumulate(S))
Tsum = list(accumulate(T))
kosu=len(X)//2
count=1
for i in range(1,kosu+1):
    Tindex = bisect.bisect_left(Tsum, i)
    if Ssum[Tindex]>=count:
        count+=1
print(len(X)-(count-1)*2)

Submission status: Accepted

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
                cur += 1
                break
            cur += 1
print(length-cnt)

Submission status: Accepted

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

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: Accepted

Solution code:
X = input().strip()
N = len(X)
cnt = 0
tot = 0
for i in range(N):
    if X[i]=="T":
        cnt += 1
    else:
        if cnt>0:
            tot += cnt
            cnt = -1
        else:
            cnt -= 1
print(2*tot)

Submission status: Accepted

Solution code:
S = str(input())
N = len(S)
ans = 0
dis = 0
for i in range(N):
  if S[i] == "T":
    if dis == 0:
      ans +=1
    else:
      dis -= 1
  else: #Sのとき
    dis += 1
ans += dis
print(ans)

Submission status: Accepted

Solution code:
x = input()
stack = []
for xx in x:
    if xx == "T":
        if len(stack) > 0:
            if stack[-1] == "S":
                stack.pop()
            else:
                stack.append(xx)
        else:
            stack.append(xx)
    else:
        stack.append(xx)

print(len(stack))

Submission status: Accepted

Solution code:
X=list(input())

counter_s = 0

counter_t = 0

for i in range(len(X)):
    if counter_s == 0 and X[i] == 'T':
        counter_t += 1
    elif 0 < counter_s and X[i] == 'T':
        counter_s -= 1
    elif X[i] == 'S':
        counter_s += 1
print(counter_s+counter_t)

Submission status: Accepted

Solution code:
import sys

X = sys.stdin.readline().strip()
lx = len(X)

stack = []
pop_n = 0
for c in X:
    if c == "S":
        stack.append(c)
    elif c == "T" and 0 < len(stack):
        stack.pop()
        pop_n += 1

print(lx - 2 * pop_n)

Submission status: Accepted

Solution code:
s = input()

ans = 0
num = 0
for c in s:
    if c == 'T':
        num -= 1
    else:
        num += 1
    if num < 0:
        ans += 1
        num = 0

ans += num
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from collections import deque


def read():
    X = input().strip()
    return X,


def solve(X):
    q = deque()
    for x in X:
        if x == "T":
            if len(q) > 0 and q[-1] == "S":
                # remove ST pair
                q.pop()
            else:
                q.append("T")
        elif x == "S":
            q.append("S")
    return len(q)
        


if __name__ == '__main__':
    inputs = read()
    print(solve(*inputs))

Submission status: Accepted

Solution code:
x = input()
total = 0
ans = 0
for s in x:
    if s == "T":
        total += 1
        ans = max(ans, total)
    else:
        total -= 1
print(ans * 2)

Submission status: Accepted

Solution code:
x = input()

stock = 0
rest = len(x)

for a in x:
    if a == "S":
        stock += 1
    else:
        if stock > 0:
            stock -= 1
            rest -= 2
        else:
            stock = 0

print(rest)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline


def main():
    S = input().rstrip()
    count = 0
    ans = 0
    for s in S:
        if s == "S":
            count += 1
        if s == "T":
            count -= 1
        if count < 0:
            ans += 1
            count = 0

    print(ans+count)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from collections import deque


def read():
    X = input().strip()
    return X,


def solve(X):
    q = deque()
    for x in X:
        if x == "S":
            q.append(x)
        elif x == "T":
            if len(q) > 0 and q[-1] == "S":
                q.pop()
            else:
                q.append(x)
    return len(q)


if __name__ == '__main__':
    inputs = read()
    print("%s" % solve(*inputs))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from collections import deque


def read():
    X = input().strip()
    return X,


def solve(X):
    ans = 0
    q = deque()
    for x in X:
        if x == "S":
            q.append(x)
        elif x == "T":
            if len(q) > 0:
                q.popleft()
            else:
                ans += 1
    ans += len(q)
    return ans


if __name__ == '__main__':
    inputs = read()
    print("%s" % solve(*inputs))

Submission status: Accepted

Solution code:
s = input()
S = 0

ans = len(s)
for i in s:
    if i == "S":
        S += 1
    else:
        if S > 0:
            ans -= 2
            S -= 1
print(ans)

Submission status: Accepted

Solution code:
x = input()
num = [0]
for i in x:
    if i == "S":
        num.append(num[-1] - 1)
    else:
        num.append(num[-1] + 1)
print(max(max(num), 0) * 2)

Submission status: Accepted

Solution code:
X = list(input())
N = len(X)
Life = 0
out = 0
for i in range(N):
    if X[i]=="S":
        Life += 1
    elif X[i]=="T":
        if Life==0:
            out += 1
        elif Life>0:
            Life += -1
print(out+Life)

Submission status: Accepted

Solution code:
x = input()

cnt = 0

while len(x) - 1 > cnt:
    if x[cnt] == 'S' and x[cnt+1] == 'T':
        x = x[:cnt] + x[cnt+2:]
        cnt = max(0, cnt - 1)
    else:
        cnt += 1

print(len(x))

Submission status: Accepted

Solution code:
X=input()

ans=0
for i in range(len(X)):
    if X[i]=='S':
        ans+=1
    else:
        if ans==0:
            ans=0
        else:
            ans-=1
print(ans*2)

Submission status: Accepted

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: Accepted

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: Accepted

Solution code:
X = input()

n = len(X)

S_cnt = 0
cnt = 0

for i in range(n):
    if X[i]=='S':
        S_cnt += 1
    elif (X[i]=='T') and(S_cnt>0):
        cnt += 1
        S_cnt -= 1
print(n-cnt*2)

Submission status: Accepted

Solution code:
X = input()
ans = len(X)
c = 0

for x in X:
    if x == 'S':
        c += 1
    else:
        if c > 0:
            ans -= 2
            c -= 1
print(ans)

Submission status: Accepted

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

for i in range(n):
    if x[i]=='S':
        left=i
        break
else:
    print(n)
    exit(0)

right=left+1
ans=n

while True:

    while right<n and x[right]=='S':
        right+=1
    
    if right>=n:
        break

    ans-=2
    right+=1
    left+=1
    while left<n and x[left]=='T':
        left+=1
    
    if left>=n:
        break

    right=max(left+1,right)


print(ans)

Submission status: Accepted

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

removed = 0
cnt_s = 0

for i in range(n):
    if s[i] == 'S':
        cnt_s += 1
    elif cnt_s:
        cnt_s -= 1
        removed += 1
#     print(cnt_s,cnt_t)
print(n - removed*2)

Submission status: Accepted

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 i == n-1:
        removed += min(cnt_s,cnt_t)
    elif cnt_t > cnt_s:
        removed += cnt_s
        cnt_s = cnt_t = 0
#     print(cnt_s,cnt_t)
print(len(s) - removed*2)

Submission status: Accepted

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

removed = 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:
        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: Accepted

Solution code:
x = str(input())
z = 0
t = 0
for xi in x:
  if xi == "S":
    z += 1
  if xi == "T":
    if z >= 1:
      z -= 1
    else:
      t += 1
print(t+z)

Submission status: Accepted

Solution code:
import sys
import math
import copy
import heapq
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter
sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = float("inf")
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)

def nck(n, k, kaijyo):
    return (npk(n, k, kaijyo) * divide(kaijyo[k])) % MOD

def npk(n, k, kaijyo):
    if k == 0 or k == n:
        return n % MOD
    return (kaijyo[n] * divide(kaijyo[n-k])) % MOD

def fact_and_inv(SIZE):
    inv = [0] * SIZE  # inv[j] = j^{-1} mod MOD
    fac = [0] * SIZE  # fac[j] = j! mod MOD
    finv = [0] * SIZE  # finv[j] = (j!)^{-1} mod MOD
    inv[1] = 1
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    for i in range(2, SIZE):
        inv[i] = MOD - (MOD // i) * inv[MOD % i] % MOD
        fac[i] = fac[i - 1] * i % MOD
        finv[i] = finv[i - 1] * inv[i] % MOD

    return fac, finv

def renritsu(A, Y):
    # example 2x + y = 3, x + 3y = 4
    # A = [[2,1], [1,3]])
    # Y = [[3],[4]] または [3,4]
    A = np.matrix(A)
    Y = np.matrix(Y)
    Y = np.reshape(Y, (-1, 1))
    X = np.linalg.solve(A, Y)

    # [1.0, 1.0]
    return X.flatten().tolist()[0]

class TwoDimGrid:
    # 2次元座標 -> 1次元
    def __init__(self, h, w, wall="#"):
        self.h = h
        self.w = w
        self.size = (h+2) * (w+2)
        self.wall = wall
        self.get_grid()
        # self.init_cost()

    def get_grid(self):
        grid = [self.wall * (self.w + 2)]
        for i in range(self.h):
            grid.append(self.wall + getS() + self.wall)

        grid.append(self.wall * (self.w + 2))
        self.grid = grid
    def init_cost(self):
        self.cost = [INF] * self.size

    def pos(self, x, y):
        # 壁も含めて0-indexed 元々の座標だけ考えると1-indexed
        return y * (self.w + 2) + x
    def getgrid(self, x, y):
        return self.grid[y][x]
    def get(self, x, y):
        return self.cost[self.pos(x, y)]
    def set(self, x, y, v):
        self.cost[self.pos(x, y)] = v
        return
    def show(self):
        for i in range(self.h+2):
            print(self.cost[(self.w + 2) * i:(self.w + 2) * (i+1)])
    def showsome(self, tgt):
        for t in tgt:
            print(t)
        return
    def showsomejoin(self, tgt):
        for t in tgt:
            print("".join(t))
        return

    def search(self):
        grid = self.grid
        move = [(0, 1), (0, -1), (1, 0), (-1, 0)]
        move_eight = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
        # for i in range(1, self.h+1):
        #     for j in range(1, self.w+1):
        #         cx, cy = j, i
        #         for dx, dy in move_eight:
        #             nx, ny = dx + cx, dy + cy

def solve():
    S = getS()
    ans  = 0
    ss = 0
    for c in S:
        if c == "S":
            ss += 1
        else:
            if ss:
                ss -= 1
            else:
                ans += 1
    print(ans * 2)

def main():
    n = getN()
    for _ in range(n):
        solve()

    return
if __name__ == "__main__":
    # main()
    solve()

Submission status: Accepted

Solution code:
X = input()
n = len(X)
S_waitng_for_T = 0
ans = n
for i in range(n):
    if X[i] == 'S':
        S_waitng_for_T += 1
    else:
        if S_waitng_for_T > 0:
            S_waitng_for_T -= 1
            ans -= 2
print(ans)

Submission status: Accepted

Solution code:
def main():
    X = input()
    cnt_s = 0
    cnt_removal = 0
    for x in X:
        if x == 'T':
            if cnt_s > 0:
                cnt_s -= 1
                cnt_removal += 1
        else:
            cnt_s += 1
    print(len(X) - 2 * cnt_removal)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
a = b = 0
for s in input():
  if s == 'S':
    a += 1
  else:
    if a == 0:
      b += 1
    else:
      a -= 1
print(a+b)

Submission status: Accepted

Solution code:
S = input()
a = b = c = 0
for s in S:
  if s == 'S':
    a += 1
  else:
    if a == 0:
      b += 1
    else:
      a -= 1
print(a+b)

Submission status: Accepted

Solution code:
def main():
    import sys

    def input(): return sys.stdin.readline().rstrip()

    x = input()            
    cnt  = 0
    t = 0
    for i in x:
        if i == 'S':
            cnt += 1
        else:
            cnt -= 1
        t = min(t, cnt)
    print(cnt-2*t)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
w = input()

mode = 1
c_s = 0
ans = 0

for i in w:
#    print(i)
    if i == 'S':
        c_s += 1
    else:
        if c_s > 0:
            ans += 1
            mode = 1
            c_s -= 1

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

Submission status: Accepted

Solution code:
X = input()

stack_s = []
out = []

for x in X:
    if x == "S":
        stack_s.append("S")
    else:
        if len(stack_s) > 0:
            stack_s.pop()
        else:
            out.append("T")

while len(stack_s) > 0:
    out.append(stack_s.pop())

print(len(out))

Submission status: Accepted

Solution code:
t=input()
n=len(t)
ans=0
s=0
for i in range(n):
    if t[i]=="S":
        s+=1
    if s>0 and t[i]=="T":
        ans+=1
        s-=1
print(n-2*ans)

Submission status: Accepted

Solution code:
X = input()
l = []
for i in range(len(X)):
    if X[i] == "S":
        l.append("S")
    elif X[i] == "T":
        if len(l) > 0 and l[-1] == "S":
            l.pop(-1)
        #  lが空 or lの末尾がT
        else:
            l.append("T")
ans = len(l)
print(ans)

Submission status: Accepted

Solution code:
X = open(0).read().split()[0]
ans = 0
s_count = 0
for x in X:
    if x == 'T':
        if s_count == 0:
            ans += 1
        else:
            s_count -= 1
    else:
        s_count += 1
print(ans+s_count)

Submission status: Accepted

Solution code:
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    X = SS()

    stk = []

    len_X = len(X)
    ans = len_X
    for i in range(len_X):
        if X[i] == 'S':
            stk.append('S')
        else:
            if stk and stk[-1] == 'S':
                stk.pop()
                ans -= 2

    print(ans)

if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

S = input().rstrip()
s_cnt =0
t_cnt =0

for s in S:
    if s == 'S':
        s_cnt += 1
    else:
        if s_cnt >0:
            s_cnt -= 1
        else:
            t_cnt += 1
            
print(t_cnt + s_cnt)

Submission status: Accepted

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

for s in S:
    if s == 'S':
        s_cnt += 1
    else:
        if s_cnt >0:
            s_cnt -= 1
        else:
            t_cnt += 1
            
print(t_cnt + s_cnt)

Submission status: Accepted

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

for s in S:
    if s == 'S':
        s_cnt += 1
    if s == 'T':
        t_cnt += 1
        if s_cnt >0:
            t_cnt -= 1
            s_cnt -= 1
print(t_cnt + s_cnt)