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 .

Solution (Python):
x = input()
s=0
ans=0
for i in x:
  if i == "S":
    s+=1
  else:
    if s==0:
      ans +=1
    else:
      s-=1
print(ans+s)

Solution (Python):
s = input()
 
l = 0
r = 0
for si in s:
  if si == "S":
    r += 1
  else:
    if r == 0:
      l += 1
    else:
      r -= 1
ans = l + r
print(ans)

Solution (Python):
X = input()

s_cnt = 0
t_cnt = 0

ans = 0

for n in range(len(X)):
  if X[n] == "S":
    s_cnt += 1
  else:
    t_cnt += 1
  ans = max(ans,t_cnt-s_cnt)
print(ans*2)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time, copy

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  

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

Solution (Python):

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)




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)
t_count = 0
s_count = 0
ans = 0
prev = X[-1]
if (prev == 'S'):
    s_count = 1
else:
    t_count = 1
for x in reversed(X[:-1]):
    if (prev == 'S' and x == 'T'):
        
        rest = max(s_count - t_count, 0)
        ans += (rest*2)
        t_count = max(t_count - s_count, 0)
        s_count = 0
    if (x == 'S'):
        s_count += 1
    if (x == 'T'):
        t_count += 1
    prev = x
print(ans)

Solution (Python):
X = input()
ans = 0
sc = 0
for x in X:
    if x == "S":
        sc += 1
    else:
        if sc:
            sc -= 1
        else:
            ans += 2
print(ans)

Solution (Python):
s=input() ; n=len(s)
ss=n//2; tt=n//2
ans=0
for i in range(n):
    if s[i]=="S":
        ans=max(ans, ss-tt)
        ss-=1
    else:
        tt-=1
print(ans*2)

Solution (Python):
def main():
    
    import sys
    input = lambda:sys.stdin.readline().strip()
    S = input()
    
    
    N = len(S)
    cnt=0
    ans=0
    for i in range(N):
        if S[i]=='S':
            cnt+=1
        else:
            if cnt:
                cnt-=1
                ans+=2
    print(N-ans)


if __name__=='__main__':
    main()

Solution (Python):
x = input()

ans = len(x)
l = 0
r = 0
for i in range(len(x)):
    if x[i] == "S":
        l += 1
    else:
        l -= 1

        if l < 0:
            l = 0
        else:
            ans -= 2

print(ans)

Solution (Python):
S = input()
N = len(S)
ans = 0
cnt = 0

for i in range(N):
    if S[i] == 'T':
        cnt += 1
    else:
        cnt -= 1
    ans = max(ans,cnt)

print(ans * 2)

Solution (Python):
X = input()
n = len(X)
cnt = 0
S_check = 0

for i in range(n):
    if X[i] == 'S':
        S_check += 1

    if X[i] == 'T':
        if S_check > 0:
            S_check -= 1
        else:
            cnt += 1

print(cnt + S_check)

Solution (Python):
x=list(input())
s=0
t=0
ans=0
for i in range(len(x)):
    if x[i]=="S":
        s+=1

    else:
        if s==0:
            ans+=1
        else:
            s-=1

print(ans+s)

Solution (Python):
x = input()
ans = []

for i in x:
    if i == 'S':
        ans.append(i)
    else:
        if len(ans) == 0 or ans[-1] == 'T':
            ans.append(i)
        else:
            ans.pop()
print(len(ans))

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

Solution (Python):
X = input()
N = len(X)
cnt = 0
ans = 0
for x in X:
  if x == "S":
    cnt += 1
  else:
    if cnt == 0:
      ans += 1
    else:
      cnt -= 1

print(ans * 2)

Solution (Python):

from collections import deque
s=input()
n=len(s)
A=deque()
for i in range(n):
    if s[i]=="S":
        A.append(s[i])
    else:
        if len(A)==0:
            A.append(s[i])
        elif A[-1]=="S":
            A.pop()
        else:
            A.append(s[i])
print(len(A))

Solution (Python):
from collections import deque

s = input()
q = deque()

for si in s:
    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))

Solution (Python):
X = input()

ans = 0
t = 0
for x in X:
  if x == "S":
    t -= 1
  else:
    t += 1
  ans = max(ans, t)

print(ans * 2)

Solution (Python):
x = list(input())
x = x[::-1]
counter = 0
ans = len(x)
for i in x:
    if i == "T":
        counter += 1
    elif i == "S" and counter:
        counter -= 1
        ans -= 2
print(ans)

Solution (Python):
X = input().rstrip()
cnt = 0
res = len(X)
for x in X:
    if x=="S":
        cnt += 1
    else:
        if cnt > 0:
            res -= 2
            cnt -= 1
print(res)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):






from collections import deque





X = input()





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

Solution (Python):
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)

Solution (Python):
s = input()
ans = len(s)
temp1 = 0 
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)

Solution (Python):
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)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 9)

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 collections import deque, defaultdict





def solve():
    X = input()
    

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

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 9)

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 collections import deque, defaultdict





def solve():
    X = input()
    

    L = deque([])
    R = deque(list(X))
    
    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()

Solution (Python):
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)

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

Solution (Python):
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

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

Solution (Python):
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(len(ans))

Solution (Python):
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))

Solution (Python):
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)

Solution (Python):
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))

Solution (Python):
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(ans)

Solution (Python):
import sys
input = sys.stdin.buffer.readline




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


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

Solution (Python):
import sys
input = sys.stdin.buffer.readline




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


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

Solution (Python):
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))

Solution (Python):



from fractions import gcd
from itertools import combinations,permutations,accumulate, product 

from collections import deque,defaultdict,Counter
import decimal
import re












import sys
sys.setrecursionlimit(10000000)


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)

Solution (Python):
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))

Solution (Python):
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))

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
s=input()
ans=0
i=0
c=-1
tmp1=0
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(x+y)

Solution (Python):
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)

Solution (Python):



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

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

Solution (Python):
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))

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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))

Solution (Python):
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)

Solution (Python):
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: 
    dis += 1
ans += dis
print(ans)

Solution (Python):
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))

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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":
                
                q.pop()
            else:
                q.append("T")
        elif x == "S":
            q.append("S")
    return len(q)
        


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

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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()

Solution (Python):
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))

Solution (Python):
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))

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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))

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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))

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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(n - removed*2)

Solution (Python):
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(len(s) - removed*2)

Solution (Python):
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

removed += min(cnt_s,cnt_t)
print(len(s) - removed*2)

Solution (Python):
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)

Solution (Python):
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 = 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  
    fac = [0] * SIZE  
    finv = [0] * SIZE  
    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):
    
    
    
    A = np.matrix(A)
    Y = np.matrix(Y)
    Y = np.reshape(Y, (-1, 1))
    X = np.linalg.solve(A, Y)

    
    return X.flatten().tolist()[0]

class TwoDimGrid:
    
    def __init__(self, h, w, wall="
        self.h = h
        self.w = w
        self.size = (h+2) * (w+2)
        self.wall = wall
        self.get_grid()
        

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

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__":
    
    solve()

Solution (Python):
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)

Solution (Python):
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()

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

Solution (Python):
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)

Solution (Python):
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()

Solution (Python):
w = input()

mode = 1
c_s = 0
ans = 0

for i in w:

    if i == 'S':
        c_s += 1
    else:
        if c_s > 0:
            ans += 1
            mode = 1
            c_s -= 1

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