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):
import sys
input = sys.stdin.readline



def main():
    X = input().strip()
    stack = list()
    for s in X:
        if s == "S" or len(stack) < 1:
            stack.append(s)
        elif s == "T" and stack[-1] == "T":
            stack.append(s)
        else:
            stack.pop()
    return len(stack)


if __name__ == "__main__":
    print(main())

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

Solution (Python):
from collections import deque
s=input()
d=[]

for i in s:
    if i=="S":
        d.append("S")
    elif i=="T" and (not d or d[-1]=="T"):
        d.append("T")
    else:
        d.pop()
print(len(d))

Solution (Python):
from collections import deque
s=input()
d=deque()

for i in s:
    if i=="S":
        d.append("S")
    elif i=="T" and (not d or d[-1]=="T"):
        d.append("T")
    else:
        d.pop()
print(len(d))

Solution (Python):
s=input()
n,ans,l=len(s),len(s),0
for c in s:
	if c=='S':
		l += 1
	elif l:
		l -= 1
		ans -= 2
print(ans)

Solution (Python):
from collections import deque

X = input()
n = len(X) 

stack = deque([])

for char in X:
	if char == 'S':
		stack.append(char)
	elif char == 'T' and len(stack) == 0:
		stack.append('T')
	elif char == 'T' and stack[-1] == 'S':
		stack.pop()
	elif char == 'T' and stack[-1] == 'T':
		stack.append('T')

print(len(stack))

Solution (Python):
from collections import deque
s=input()

A=deque([])
for i in s:
    if len(A)<1:
        A.append(i)
    else:
        if A[-1]+i=="ST":A.pop()
        else:A.append(i)

print(len(A))

Solution (Python):
X = input()

left_S = 0
left_T = 0

for i in range(len(X)):
    if X[i] == "S":
        left_S += 1
    elif left_S == 0:
        left_T += 1
    else:
        left_S -= 1

print(left_S + left_T)

Solution (Python):
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split()) 
ins = lambda : input().strip()
DBG = True 
BIG = 10**18
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

x = ins()
s = []
for c in x:
  s.append(c)
  while len(s)>1 and s[-2]=='S' and s[-1]=='T':
    s.pop()
    s.pop()
print(len(s))

Solution (Python):
from itertools import groupby as gb
l=[(a,len(list(b))) for a,b in gb(list(input()))]
ans=0
now=0
if l[0][0]=="T":
    ans+=l[0][1]
    now+=1
pres=0
for (k,x),(k1,y) in zip(l[now::2],l[now+1::2]):
    
    if x+pres>=y:pres=(x+pres-y)
    else:
        ans+=y-x-pres
        pres=0
print(ans+pres+(0 if l[-1][0]=="T" else l[-1][1]))

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

Solution (Python):
X = input()
p = X[0]
ls = []
cnt = 1
for c in X[1:]:
  if p!=c:
    ls.append(cnt)
    cnt = 1
    p = c
  else:
    cnt += 1
ls.append(cnt)
s = 0
t = 0
m = 0
if X[0]=='S':
  for i in range(len(ls)):
    if i%2==0:
      s += ls[i]
    else:
      t += ls[i]
    if s<t:
      m += t-s
      t = 0
      s = 0
  print(2*m)
else:
  for i in range(len(ls)):
    if i%2==0:
      t += ls[i]
    else:
      s += ls[i]
    if s<t:
      m += t-s
      t = 0
      s = 0
  print(2*m)

Solution (Python):
s=input()

s=list(s)

ans = ['G']

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

Solution (Python):




from collections import deque

stack = deque([])
X = input()

for ch in X:
    if ch == 'S':
        stack.append(ch)
    elif ch == 'T':
        if len(stack) == 0:
            stack.append(ch)
        elif stack[-1] == 'T':
            stack.append(ch)
        elif stack[-1] == 'S':
            stack.pop()

print(len(stack))

Solution (Python):
s = input()
s_cnt = puyo = 0
for si in s:
    if si == "S": s_cnt += 1
    elif s_cnt > 0:
        s_cnt -= 1
        puyo += 1
print(len(s) - puyo * 2)

Solution (Python):
from collections import deque
x = input()
n = len(x)
st = deque()

for i in range(n):
    if x[i] == "S":
        st.append("S")
    else:
        if st == deque([]):
            st.append("T")
        else:
            a = st.pop()
            if a == "T":
                st.append("T")
                st.append("T")
            else:
                continue
                
print(len(st))

Solution (Python):
def agc005_a():
  x = str(input())
  s_accum = [0]
  t_accum = [0]
  t_up = 0
  for ch in x:
    
    if ch == 'S':
      s_accum.append(s_accum[-1] + 1)
      t_accum.append(t_accum[-1])
    else:
      s_accum.append(s_accum[-1])
      t_accum.append(t_accum[-1] + 1)
    
    if ch == 'T':
      t_up = max(t_up, t_accum[-1] - s_accum[-1])
  ans = 2 * t_up
  print(ans)

agc005_a()

Solution (Python):
from itertools import groupby
import heapq

X=input()
N=len(X)
X=[X[i] for i in range(0,N)]
X=groupby(X)
data=[]
i=0
for key,group in X:
    g=len(list(group))
    data.append((i,key,g))
    i+=1

if data[0][1]=='T':
    data=data[1:]
if data[-1][1]=='S':
    data=data[:len(data)-1]


heapq.heapify(data)
left=0
ans=0
while data:
    S=heapq.heappop(data)
    T=heapq.heappop(data)
    s=S[2]+left
    t=T[2]
    if s>=t:
        left=s-t
        ans+=t
    else:
        left=0
        ans+=s

print(N-2*ans)

Solution (Python):
import sys
import math  
import bisect  
import queue  


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


def main():
    X = input()

    que = queue.LifoQueue()
    cnt = 0
    rest_T = 0
    for x in X:
        if x == 'S':
            que.put(x)
            cnt += 1
        elif x == 'T' and cnt > 0:
            _ = que.get()
            cnt -= 1
        else:
            rest_T += 1

    print(rest_T + cnt)


if __name__ == '__main__':
    main()

Solution (Python):
k = 0
for i in input():
  k += i=="S" or -(k>0)
print(k*2)

Solution (Python):


S = input()
ret = len(S)
stack = []
for s in S:
    if s == 'S':
        stack.append(S)
    if s == 'T' and stack:
        stack.pop()
        ret -= 2

print(ret)

Solution (Python):
from collections import deque
X = input()

scan = deque()

for c in X:
    
    if c == 'S':
        scan.append(c)
    else:
        if not scan or scan[-1] == 'T':
            scan.append('T')
        else:
            scan.pop()
print(len(scan))

Solution (Python):
from collections import deque
X = input()

scan = deque()

for c in X:
    
    if c == 'S':
        scan.append(c)
    else:
        if not scan or scan[-1] == 'T':
            scan.append('T')
        else:
            scan.pop()
print(len(scan))

Solution (Python):
X = input()
N = len(X)

right = 1
cnt = 0

for left in range(N - 1):
    if X[left] == 'S':
        right = max(right, left + 1)
        while right < N and X[right] != 'T':
            right += 1
        if right >= N:
            break
        if X[right] == 'T':
            
            right += 1
            cnt += 1
print(N - cnt * 2)

Solution (Python):
s=input()
cnt,ans=0,len(s)
for ss in s:
  if ss=="S":
    cnt+=1
  else:
    if cnt>0:
        cnt-=1
        ans-=2
print(ans)

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

Solution (Python):
def main():
    S = input()
    cnt = 0
    ans = len(S)
    for s in S:
        if s == 'S':
            cnt += 1
        elif s == 'T':
            if cnt:
                cnt -= 1
                ans -= 2
        else:
            cnt = 0
    
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
S = str(input())
ans = 0
s_ctr = 0

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

Solution (Python):
s=0
for x in input():s+=x<'T'or-(s>0)
print(2*s)

Solution (Python):
t,s=0,0
for x in input():
 if x=='S':s+=1
 elif s:s-=1
 else:t+=1
print(t+s)

Solution (Python):
X = list(input())
nt, ns = 0, 0
for x in X:
    if x == 'S':
        ns += 1
    elif ns > 0:
        ns -= 1
    else:
        nt += 1
print(nt+ns)

Solution (Python):
X = list(input())
N = len(X)
NHALF = N // 2

def solve():
    nt, ns = 0, 0
    for i in range(N):
        if X[i] == 'S':
            ns += 1
        elif ns > 0:
            ns -= 1
        else:
            nt += 1
    return nt + ns


print(solve())

Solution (Python):
X=input()

ans=0
a=0
for i in range(len(X)):
  if(X[i]=="T"):
    a+=1
  else:
    a-=1
  ans=max(ans,a)
print(ans*2)

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for _ in range(j)] for _ in range(k)]




S = input()
'''
s_cnt = 0
for m in S:
    if m == 'S':
        s_cnt += 1
    elif m == 'T' and s_cnt > 0:
        s_cnt -= 1

print(2 * s_cnt)
'''
from collections import deque
ans = deque()
for m in S:
    if m == 'S':
        ans.append(m)
    elif m == 'T':
        if ans and ans[-1] == 'S':
            ans.pop()
        else:
            ans.append(m)
print(len(ans))

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for _ in range(j)] for _ in range(k)]




S = input()
'''
s_cnt = 0
for m in S:
    if m == 'S':
        s_cnt += 1
    elif m == 'T' and s_cnt > 0:
        s_cnt -= 1

print(2 * s_cnt)
'''
ans = []
for m in S:
    if m == 'S':
        ans.append(m)
    elif m == 'T':
        if ans and ans[-1] == 'S':
            ans.pop(-1)
        else:
            ans.append(m)
print(len(ans))

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for _ in range(j)] for _ in range(k)]




S = input()
'''
f_cnt = b_cnt = 0
for m in S:
    if m != 'T':
        break
    f_cnt += 1

for m in S[::-1]:
    if m != 'S':
        break
    b_cnt += 1
print(max(f_cnt, b_cnt) * 2)
'''
s_cnt = 0
for m in S:
    if m == 'S':
        s_cnt += 1
    elif m == 'T' and s_cnt > 0:
        s_cnt -= 1

print(2*s_cnt)

Solution (Python):
import sys


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


sys.setrecursionlimit(10 ** 9)


def main():
    X = input()
    answer = len(X)
    cnt_s = 0
    for x in X:
        if x == "S":
            cnt_s += 1
            continue
        if cnt_s > 0:
            cnt_s -= 1
            answer -= 2
            
    print(answer)


if __name__ == "__main__":
    main()

Solution (Python):
import sys


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


sys.setrecursionlimit(10 ** 9)


def main():
    X = input()
    answer = 0
    cnt_s = 0
    for x in X:
        if x == "S":
            cnt_s += 1
            continue
        if cnt_s > 0:
            cnt_s -= 1
        else:
            answer += 1
    answer += cnt_s
    print(answer)


if __name__ == "__main__":
    main()

Solution (Python):
X = input()
N = len(X)

cnt_s = 0
ans = N
for i in range(N):
    if X[i] == "S":
        cnt_s += 1
    else:
        if cnt_s > 0:
            ans -= 2
            cnt_s -= 1

print(ans)

Solution (Python):
from collections import deque
S = input()
d = deque()
for s in S:
  if len(d) == 0:
    d.append(s)
  elif s == "T":
    d.pop() if d[-1] == "S" else d.append(s)
  elif s == "S":
    d.append(s)
print(len(d))

Solution (Python):
from collections import deque
S = input()
d = deque()
for s in S:
  if len(d) == 0:
    d.append(s)
  elif s == "T":
    if d[-1] == "S":
      d.pop()
    else:
      d.append(s)
  elif s == "S":
    d.append(s)
print(len(d))

Solution (Python):
x = input()
count = 0
ans = len(x)
for i in range(len(x)):
    if x[i] == "S":
        count += 1
    else:
        if count > 0:
            count -= 1
            ans -= 2
print(ans)

Solution (Python):
X = input()
def solve(X):
    ns = 0
    nt = 0
    for x in X:
        if x == 'S':
            ns += 1
        elif ns>0:
            ns -= 1
        else:
            nt += 1
    return ns+nt
print(solve(X))

Solution (Python):
X = input()
s_cnt, t_cnt = 0, 0
for i in range(len(X)):
  if X[i] == "S":
    s_cnt += 1
  elif s_cnt != 0:
    s_cnt -= 1
    t_cnt += 1
print(len(X) - 2*t_cnt)

Solution (Python):


import sys
import os
import re
import datetime
import bisect



X = input()

restS = []
for c in X:
    
    restS.append(c)
    if c == 'T':
        while(len(restS) >= 2):
            
            
            if(restS[-2] == 'S' and restS[-1] == 'T'): 
                
                
                del restS[-2:]
                
            else:
                
                break 

print(len(restS))

Solution (Python):


import sys
import os
import re
import datetime
import bisect



X = input()

restS = []
for c in X:
    
    restS.append(c)
    if c == 'T':
        while(len(restS) >= 2):
            
            if("".join(restS[-2:]) == 'ST'):
                
                
                del restS[-2:]
                
            else:
                
                break 

print(len(restS))

Solution (Python):
def main():
    x = input()

    s = 0
    count = 0
    for cur in x:
        if cur == "S":
            s += 1
        else:
            if s != 0:
                s -= 1
            else:
                count += 2

    print(count)


if __name__ == '__main__':
    main()

Solution (Python):
X=input()
stack=[]
point=0
for i in range(len(X)):
    if X[i]=="S":
        stack.append(X[i])
        point+=1
    else:
        if point==0:
            stack.append(X[i])
            point+=1
        elif stack[point-1]=="T":
            stack.append(X[i])
            point+=1
        elif stack[point-1]=="S":
            del stack[point-1]
            point-=1
print(len(stack))

Solution (Python):
X = input()
s_cnt = 0
ans = len(X)
for i in range(len(X)):
    if(X[i] == "S"):
        s_cnt += 1
    else:
        if(s_cnt > 0):
            ans -= 2
            s_cnt -= 1
print(ans)

Solution (Python):
from collections import deque

s = input()
n = len(s)
que = deque()
for i in range(n):
    if s[i] == 'S':
        que.append(s[i])
    else:
        if len(que) == 0:
            que.append(s[i])
        elif que[-1] == 'T':
            que.append(s[i])
        else:
            que.pop()
    
print(len(que))

Solution (Python):
s = input()[::-1]
n = len(s)
tmp = 0
ans = 0
for i in range(n):
    if s[i] == 'S':
        tmp += 1
    else:
        tmp -= 1
    ans = max(ans, tmp)

print(ans * 2)

Solution (Python):
if __name__ == '__main__':
    x = list(input())
    sl = []
    ans = 0
    for i in x:
        if i == "S":
            sl.append(1)
        else:
            if len(sl) != 0:
                ans += sl.pop()

    print(len(x) - ans * 2)

Solution (Python):









X = input()


stack = []
n_poped = 0
for x in X:
    if x == 'S':
        stack.append(1)
    elif x == 'T':
        if stack:
            stack.pop()
            n_poped += 2

print(len(X) - n_poped)

Solution (Python):
x = input()
stack = []
for s in x:
    if s == 'S':
        stack.append(s)
    else:
        if not stack:
            stack.append(s)
        elif stack[-1] == 'S':
            stack.pop()
        else:
            stack.append(s)
print(len(stack))

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

Solution (Python):
x=input()
l=len(x)
ans=0
q=[]
for i in range(l):
    if x[i]=='S':
        q.append(x[i])
    else:
        if not q or q[-1]=='T':
            q.append(x[i])
        else:
            q.pop()
            ans+=1
print(l-ans*2)

Solution (Python):
x=input()
l=len(x)
c=0
ans=0
for i in range(l):
    if x[i]=='S':
        c+=1
    else:
        if c>0:
            ans+=1
        c-=1
    if c<0:
        c+=1
print(l-ans*2)

Solution (Python):
if __name__ == "__main__":
    x = input()

    count = 0
    ans = len(x)
    for i in range(len(x)):
        if x[i] == 'S':
            count += 1
        elif count > 0 and x[i] == 'T':
            ans -= 2
            count -= 1
        else:
            count = 0

    print(ans)

Solution (Python):
x = list(input())
s, t = 0, 0
ans = 0
for i in range(len(x)):
    if x[i] == "T":
        t += 1
        if s > 0:
            s -= 1
            t -= 1
    else:
        s += 1
print(s + t)

Solution (Python):
X = input()
ans = []
for x in X:
    if x == 'S':
        ans.append(x)
    else:
        if len(ans) != 0:
            if ans[-1] == 'S':
                ans.pop()
            else:
                ans.append(x)
        else:
            ans.append(x)
print(len(ans))

Solution (Python):

X = str(input())
ans = []
for x in X:
    if x == 'S':
        ans.append(x)
    else:
        if len(ans) != 0:
            if ans[-1] == 'S':
                ans.pop()
            else:
                ans.append(x)
        else:
            ans.append(x)
print(len(ans))

Solution (Python):
def resolve():
    X = input()
    stack = []
    for s in X:
        if not stack:
            stack.append(s)
            continue
        if s == 'T' and stack[-1] == 'S':
            stack.pop()
        else:
            stack.append(s)
    print(len(stack))

resolve()

Solution (Python):
X = input()
s, t = 0, 0
for i in X:
    if i=='S':
        s += 1
    else:
        if s==0:
            t += 1
        else:
            s-= 1
print(s+t)

Solution (Python):
X = input()
s_count = 0
ans = 0
for i in X:
    if i == 'S':
        s_count += 1
    elif s_count > 0:
        s_count -= 1
        ans += 2
print(len(X)-ans)

Solution (Python):
from collections import deque


def main():
    word = input()
    stack = deque()
    for w in word:
        if w == "S":
            stack.appendleft(w)
        elif w == "T" and (len(stack) == 0 or stack[0] == "T"):
            stack.appendleft(w)
        else:
            stack.popleft()
    print(len(stack))


if __name__ == '__main__':
    main()

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

Solution (Python):
X = input()
"""
Tの前にSがどれだけあるか？
TSSTTTSS
"""
s_cnt = 0
t_cnt = 0
cnt = 0
for s in X:
    if s == "S":
        s_cnt += 1
    if s == "T":
        t_cnt += 1
        if t_cnt <= s_cnt:
            cnt += 2
        else:
            s_cnt = 0
            t_cnt = 0


print(len(X) - cnt)

Solution (Python):
X = str(input())
ans = []
for x in X:
    if x == 'S':
        ans.append(x)
    else:
        if len(ans) != 0:
            if ans[-1] == 'S':
                ans.pop()
            else:
                ans.append(x)
        else:
            ans.append(x)
print(len(ans))

Solution (Python):
x = input()
ans = []
for i in range(len(x)):
    if (x[i] == "S"):
        ans.append("S")
    else:
        if (len(ans) != 0):
            if (ans[-1] == "S"):
                ans.pop(-1)
            else:
                ans.append("T")
        else:
            ans.append("T")
print(len(ans))

Solution (Python):
x = input()

t = 0
s = 0

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

print(s+t)

Solution (Python):
x = list(input())
s_cnt = [0] * (len(x)+1)

lost_pair = 0

for i in range(1, len(x)+1):
    if x[i-1] == 'S':
        s_cnt[i] = s_cnt[i-1] + 1
    else:
        if s_cnt[i-1] > 0:
            s_cnt[i] = s_cnt[i-1] - 1
            lost_pair += 1

print(len(x) - 2*lost_pair)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

X = input()

num_s = 0
num_t = 0

ans = 0
for x in X:
	if x == "T" and num_s:
		ans += 1
		num_s -= 1
	if x == "S":
		num_s += 1
print(len(X)-2*ans)

Solution (Python):
S = input()
cnt = []
c = S[0]
tmp = 0
for i in range(len(S)):
	if S[i] == c:
		tmp += 1
	else:
		cnt.append(tmp)
		c = S[i]
		tmp = 1
cnt.append(tmp)

cnt_S, cnt_T = 0,0
delete = 0
if S[0] != 'S':
	cnt.pop(0)
cnt_S = cnt[0]
for i in range(1,len(cnt),2):
	if cnt_S >= cnt[i]:
		cnt_S -= cnt[i]
		delete += cnt[i]*2
	else:
		delete += cnt_S*2
		cnt_S = 0

	if i+1 >= len(cnt):
		break

	cnt_S += cnt[i+1]

print(len(S)-delete)

Solution (Python):
X = input()
cnt = 0
res = 0
for i in X:
  if i == 'S':
    cnt += 1
  else:
    if cnt > 0:
      cnt -= 1
    else:
      res += 1
print(cnt + res)

Solution (Python):
x = input()

stack = ["X"]
for e in x:
    stack.append(e)
    if stack[-2] == "S" and stack[-1] == "T":
        stack.pop()
        stack.pop()

ans = len(stack) - 1
print(ans)

Solution (Python):
X = input()
Sn = 0
Tn = 0
for ch in X:
    if ch == "S":
        Sn += 1
    if ch == "T":
        if Sn > 0:
            Sn -= 1
        else:
            Tn += 1
print(Sn + Tn)

Solution (Python):

import sys

def INT(): return int(input())
def MAP(): return map(int,input().split())
def LI(): return list(map(int,input().split()))

def main():
    X = input()

    LENX = len(X)

    T_count = 0
    answer = LENX
    for i in range(LENX-1,-1,-1):
        if X[i] == "T":
            T_count += 1
        else:
            if T_count > 0 :
                T_count -= 1
                answer -= 2
        
    print(answer)

    return

if __name__ == '__main__':
    main()

Solution (Python):
S = input()
lS = len(S)
l = []
c = -1
for i in range(lS):
    if (c == -1):
        l.append(S[i])
        c += 1
    elif (S[i] == "S"):
        l.append(S[i])
        c += 1
    elif (S[i] == "T"):
        if (l[c] == "S"):
            l.pop()
            c -= 1
        else:
            l.append(S[i])
            c += 1
            
print(c+1)

Solution (Python):
def main():
    s = input().rstrip()
    t = [-1]*len(s)
    for i in range(len(s)):
        if s[i] == "T":
            t[i] = 1
    for i in range(1, len(s)):
        t[i] += t[i-1]
    print(2*max(t))

if __name__ == "__main__":
    main()

Solution (Python):
import itertools
s = input()
ans = len(s)
a = 0
for key,group in itertools.groupby(s):
  b = len(list(group))
  if key=="S":
    a += b
  elif key=="T":
    if a<=b:
      ans -= a*2
      a = 0
    else:
      ans -= b*2
      a = a-b
print(ans)

Solution (Python):
from collections import deque
X = input()
L = deque([])
for e in X:
    if e == "T" and len(L) > 0 and L[-1] == "S":
        L.pop()
    else:
        L.append(e)

print(len(L))

Solution (Python):
a=input()
n=len(a)
t_count=[0]*n
cnt=a.count('T')
for i in range(n):
    t_count[i]=cnt
    if a[i]=="T":
        cnt-=1

ans=0
s_used=0
t_owe=0
for i in range(n):
    if a[i]=="S":
        if t_count[i]-t_owe>0:
            ans+=2
            s_used+=1
            t_owe+=1
    else:
        if t_owe>0:
            t_owe-=1
    
print(n-ans)

Solution (Python):
x=input()

cnt={'S':0,'ST':0}
for i in x:
    if i=='T':
        if cnt['S']!=0:
            cnt['ST']+=1
            cnt['S']+=-1
    else:
        cnt[i]+=1
print(len(x)-2*cnt['ST'])

Solution (Python):
X = input()
box = []

for i in range(len(X)):
  if X[i] == "S": box.append(X[i])
  elif X[i] == "T":
    if len(box) == 0 or box[-1] != "S":
      box.append(X[i])
    else: box.pop(-1)
print(len(box))

Solution (Python):

X = input()
N = len(X)
t = 0
ans = N
for c in X[::-1]:
    if c == 'T':
        t += 1
    elif t > 0:
        ans -= 2
        t -= 1
print(ans)

Solution (Python):
if __name__ == "__main__":
    s = input()

    ans = ''

    for ss in s:
        if ss == 'S':
            ans += ss
        else:
            if len(ans) == 0:
                ans += ss
            elif ans[len(ans) - 1] == 'S':
                ans = ans[:-1]
            elif ans[len(ans) - 1] == 'T':
                ans += ss

    print(len(ans))

Solution (Python):
s = input().strip()
x=0
r=0
for c in s:
    if c == "S":
        x += 1
    else:
        if x != 0:
            x -= 1
            r += 2
print(len(s)-r)

Solution (Python):
def solve():
    x = input()
    ans = len(x)
    s = 0
    for c in x:
        if c == 'S':
            s += 1
        else:
            if s > 0:
                s -= 1
                ans -= 2
    print(ans)


if __name__ == "__main__":
    solve()

Solution (Python):
import sys
def main():
    input = sys.stdin.readline
    X = input().rstrip()
    
    ans = len(X)
    s_cnt = 0
    for x in X:
        if x == 'S': s_cnt += 1
        elif s_cnt > 0:
            s_cnt -= 1
            ans -= 2
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd

def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))

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

x = STR()
l = len(x)
stack = []
for i in range(l):
    if x[i] == 'S':
        stack.append('S')
    elif not len(stack) or stack[len(stack)-1] == 'T':
        stack.append('T')
    else:
        stack.pop()
print(len(stack))

Solution (Python):
X = input()

"""
Tの左側にSがあるなら操作をする
最終的に
TTTT....TSSSS....S
となる
"""

cnt = 0
Scnt = 0
for x in X:
    if x == "S":
        Scnt += 1
    else:
        if Scnt > 0:
            Scnt -= 1
            cnt += 2

print(len(X) - cnt)

Solution (Python):
S=input()
N=len(S)
cnt=0
ans=N
for i in range(N):
  if S[i]=='S':
    cnt+=1
  else:
    if cnt>0:
      cnt-=1
      ans-=2
print(ans)

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

Solution (Python):
S = input()

cnt = 0
remove = 0
for s in S:
    if s == "S":
        cnt += 1
    else:
        if cnt > 0:
            remove += 2
            cnt -= 1

ans = len(S) - remove
print(ans)

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

s = input()
n = len(s)
res = 0
cnt = 0
for t in s:
    if t == 'S':
        cnt += 1
    else:
        if cnt > 0:
            cnt -= 1
        else:
            res += 1
print(res+cnt)

Solution (Python):
s=str(input())
co=int(1)
n=len(s)
A=[]
for i in range(1,n):
    if s[i]!=s[i-1]:
        A.append(co)
        co=1
    else:
        co+=1
A.append(co)
m=len(A)
how=0
if s[0]=='T':
    how=1

S=int(0)
T=int(0)
for i in range(how,m-how):
    if i%2==how:
        S+=A[i]
    else:
        T+=A[i]
        if S<T:
            n-=2*S
            T=0
            S=0
        else:
            n-=2*T
            S-=T
            T=0


print(n)

Solution (Python):
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


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


def print_matrix(mat):
    for i in range(len(mat)):
        print(*mat)


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YNL = {False: 'No', True: 'Yes'}
YNU = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False



def main():
    X = S()
    stack = deque()
    c = 0

    for i in range(len(X)):
        if X[i] == 'S':
            stack.append(X[i])
        elif X[i] == 'T' and len(stack) != 0:
            stack.pop()
            c += 1

    print(len(X) - c * 2)


if __name__ == '__main__':
    main()

Solution (Python):
s=input()
n = len(s)

impossible = 0
num_s = 0

for v in s:
    if v=="S":
        num_s += 1
    else:
        if num_s==0:
            impossible += 2
        else:
            num_s -= 1

print(impossible)