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:
import sys
sys.setrecursionlimit(10**6)
if sys.platform in (['ios','darwin','win32']):
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

def main():
	S = input().rstrip()
	S = [s for s in S]

	# print(S)
	scnt = 0
	remove = 0
	for s in S:
		if s == 'S':
			scnt += 1
		else:
			if scnt:
				scnt -= 1
				remove += 2
	
	# print(remove)
	print(len(S)-remove)

if __name__ == '__main__':
	main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x = input()

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

Submission status: Accepted

Solution code:
x = input()

st_sub = 0
count = 0
for char in x:

    if st_sub < 0 and char == "S":
        st_sub = 1
    elif char == "S":
        st_sub += 1
    else:
        st_sub -= 1
    
    if st_sub < 0:
        count += 1
    
print(count * 2)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
x = list(input())[: -1]
s = 0
t = 0
res = len(x)
for i in range(len(x)):
  if x[i] == "S":
    s += 1
  else:
    t += 1
    if s > 0:
      res -= 2
      s -= 1
print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7 # 998244353
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    X=input()
    Q=[]
    ans=0
    for c in X:
        if(c=='T' and Q and Q[-1]=='S'): Q.pop()
        else: Q.append(c)
    print(len(Q))
resolve()

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

X = read().rstrip().decode('utf-8')

stack = ['$']
for x in X:
    if stack[-1] == 'S' and x == 'T':
        stack.pop()
        continue
    stack.append(x)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s = input()
ans = 0
tmp = 0
for i in range(len(s)):
  if s[i]=="T":
    tmp+=1
  if s[i]=="S":
    tmp-=1
  ans = max(ans,tmp)
print(ans*2)

Submission status: Accepted

Solution code:
S=input()
s,t=0,0
for c in S:
  if c=='T':
    if s>0:
      s-=1
    else:
      t+=1
  else:
    s+=1
print(s+t)

Submission status: Accepted

Solution code:
x = input()
len_x = len(x)
cnt = 0
cur = 0
for _x in x:
    if _x == "S":
        cur += 1
    else:
        cnt += cur > 0
        cur = max(0, cur - 1)
print(len_x - 2 * cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
X = input()
c = 0
d = -1
for i in range(len(X)):
  if X[i] == 'S':
    c += 1
    if c == 1:
      d = 0
  if d >= 0 and X[i] == 'T':
    c -= 1
    if c == 0:
      d = -1
print(c * 2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque

x = input()
n = len(x)

deq = deque()

for i in range(n):
    if x[i] == 'S':
        deq.appendleft(x[i])
    elif x[i] == 'T' and  deq and deq[0] == 'S':
        deq.popleft()
    else:
        deq.appendleft('T')

print(len(deq))

Submission status: Accepted

Solution code:
from collections import deque
X = input()
n = len(X)

q = deque()
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()

ans = len(q)
print(ans)

Submission status: Accepted

Solution code:
x = list(input())

ans = 0
temp = 0
for c in x:
    if c == 'T':
        if temp > 0:
            temp -= 1
        else:
            ans += 1
    else:
        temp += 1

print(ans * 2)

Submission status: Accepted

Solution code:
def sol():
  ti = 0
  ans = lenX
  for i in range(lenX//2):
    while S[i]>=T[ti]:
      ti += 1
      if ti>=lenX//2:return ans
    ans -= 2    
    ti += 1
    if ti>=lenX//2:return ans
  return ans

X = input()
lenX = len(X)
S = []
T = []
for i,x in enumerate(X):
  if x == 'S':S.append(i)
  else:T.append(i)

print(sol())

Submission status: Accepted

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

if(c>10**5):
    print(len(S)-(2*(10**5)))
else:
    print(len(S)-(2*c))

Submission status: Accepted

Solution code:
arr = list(input())
ans = 0
ct= 0
cs= 0
for i in range(len(arr)):
    if arr[i] == "S":
        cs +=1
        
    
    else:
        
        if cs >0:
            ans +=1
            cs -=1
           

print(len(arr)-2*ans)

Submission status: Accepted

Solution code:
x=input()
f=0
c=0
#なかなか良い解法、flag立てる
for i in x:
    if i=="S":
        f+=1
    else:
        if f==0:
            c+=1
        else:
            f-=1
print(2*c)

Submission status: Accepted

Solution code:
x=input()
f=0
c=0
#なかなか良い解法、flag立てる
for i in x:
    if i=="T":
        if f==0:
            c+=1
        else:
            f-=1
    else:
        f+=1
print(2*c)

Submission status: Accepted

Solution code:
x=input()
f=0
#なかなか良い解法、flag立てる
for i in x:
    if i=="S":
        f+=1
    elif f!=0:
        f-=1
print(2*f)

Submission status: Accepted

Solution code:
x=input()
f=0
#なかなか良い解法、flag立てる
for i in x:
    if i=="S":
        f+=1
    else:
        if f!=0:
            f-=1
print(2*f)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s=input()
a,b,n=0,0,len(s)
for i in range(n):
    if s[i]=='T':
        if a==0:
            b+=1
        else:
            a-=1
            b-=1
    else:
        a+=1
        b+=1
print(b)

Submission status: Accepted

Solution code:
x = input()

s = 0
t = 0
nokori = 0
disappear = 0

for i in x:
    if i == "S":
        s += 1
    else:
        if s > 0:
            disappear += 2
            s -= 1

print(len(x)-disappear)

Submission status: Accepted

Solution code:
X = input()

S, T = 0, 0
for x in X :
    if x == 'T' and S > 0 :
        S -= 1
    elif x == 'T' :
        T += 1
    elif x == 'S' :
        S += 1
        
print(S + T)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
S = list(input())[: -1]
s = 0
t = 0
res = len(S)
if S[0] == "S": s += 1
for i in range(1, len(S)):
  if S[i] == "S":
    s += 1
  else:
    if s > 0:
      res -= 2
      s -= 1
      t = 1
print(res)

Submission status: Accepted

Solution code:
# リストをスタックに使う
stack = []
X = str(input())
for i in range(len(X)):
    if X[i] == "S":
        stack.append(X[i])
    elif X[i] == "T" and (len(stack) == 0 or stack[-1] == "T"):
        stack.append(X[i])
    elif X[i] == "T" and stack[-1] == "S":
        stack.pop()
print(len(stack))

Submission status: Accepted

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

Submission status: Accepted

Solution code:

#
X = input()

buf = []
for i in range(len(X)):
    if X[i] == 'S':
        buf.append('S')
    else:
        if len(buf) > 0 and buf[-1] == 'S':
            buf.pop()
        else:
            buf.append('T')



print(len(buf))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    S = input()
    stack = 0
    for s in S:
        if s == "S":
            stack += 1
        else:
            if stack > 0:
                stack -= 1
    print(stack*2)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    S = input()
    stack = []
    for s in S:
        if s == "S":
            stack.append(s)
        else:
            if stack:
                stack.pop()
    print(2*len(stack))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input()

li = []
for i in range(len(s)):
    #print(li)
    if s[i] == "S":
        li.append(s[i])
    elif s[i] == "T":
        if len(li) >=1 and li[-1] == "S":
            li.pop()
        elif len(li) == 0 or li[0] =="T":
            li.append(s[i])
print(len(li))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x = str(input())

r = 0
result = 0
for i in range(len(x)):
    if x[i] == 'S':
        r += 1
    else:
        if r > 0:
            r -= 1
        else:
            result += 1
print(r+result)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
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))

x = input()
n = len(x)
s = 0
d = 0
for i,j in enumerate(x):
    if j == 'S':
        s += 1
    else:
        if s > 0:
            d += 2
            s -= 1
print(n-d)

Submission status: Accepted

Solution code:
x = input()

ans = len(x)
tmp_s = 0
mae = 1

for i in range(len(x)):
    if x[i] == 'S':
        #if mae == 0:
        #    tmp_s = 1
        tmp_s += 1
    elif tmp_s > 0:
        ans -= 2
        tmp_s -= 1
        mae = 0
        
print(ans)

Submission status: Accepted

Solution code:

X = input()

T = 0
S = 0

for i in X:

    if i == "S":
        S += 1

    else:
        if S > 0:
            S -= 1
        else:
            T += 1

print (S+T)

Submission status: Accepted

Solution code:
import bisect
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353

S = sys.stdin.buffer.readline().decode().rstrip()
N = len(S)


def test(S):
    s = S[:]
    while 'ST' in s:
        s = s.replace('ST', '')
        print(s)
    return len(s)


S = np.array(list(S), dtype='U1')
st = np.zeros(N, dtype=int)
st[S == 'S'] = 1
st[S == 'T'] = -1
print(min(st.cumsum()) * -2)

Submission status: Accepted

Solution code:
S = input()
s = []
for i in range(len(S)):
    if S[i] == 'S':
        s.append('S')
    else:
        if len(s) == 0 or s[-1] == 'T':
            s.append('T')
        else:
            s.pop()
print(len(s))

Submission status: Accepted

Solution code:
A = input()
n = len(A)
A += "S"
sc, tc, c = 0, 0, 0
for i in range(n):
  if A[i] == "S":
    sc += 1
  else:
    tc += 1
    if A[i+1] == "S":
      if sc <= tc:
        c += sc
        sc = 0
        tc = 0
      else:
        c += tc
        sc -= tc
        tc = 0
print(n-2*c)

Submission status: Accepted

Solution code:
# Tを1、Sを-1としてcumsum()をとる　最大値の分だけTが左に残る
import numpy as np

X = input()

Y = np.zeros(len(X), dtype=int)
for i in range(len(X)):
    if X[i] == 'S':
        Y[i] = -1
    else:
        Y[i] = 1

answer = 2 * Y.cumsum().max()
print(answer)

Submission status: Accepted

Solution code:
x = input()

stack = []
cnt = 0
for c in x:
  #stackになかったら、抜き出した文字(S or T)を追加
  if not stack:
    stack.append(c)

  else:
    #抜き出したもの(c)が"S"だった場合
    if c == "S":
      #stackに追加
      stack.append(c)

    #抜き出したもの(c)が"T"だった場合
    else:
      #末尾をstackから削除しつつ、その末尾のものをtopと定義する
      top = stack.pop()

      #topが"S",cが"T"でちゃんと対応していた場合cntを2増やす
      if top == 'S' and c == 'T':
        cnt += 2
        
print(len(x)-cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
X = input()
ans = len(X)
N = len(X)
cnt_s = 0
cnt_t = 0
for i in range(N):
    if X[i] == "S":
        cnt_s += 1
    if X[i] == "T":
        cnt_t += 1
    
    if i < N -1:
        if X[i] == "T" and X[i + 1] == "S":
            ans -= 2 * min(cnt_s, cnt_t)
            cnt_s -= min(cnt_s, cnt_t)
            cnt_t = 0
    if i == N - 1:
        ans -= 2 * min(cnt_s, cnt_t)
    
print(ans)

Submission status: Accepted

Solution code:
X = list(input())
rm = 0
cntS = 0
for x in X:
  if x == "S":
    cntS += 1
  else:
    if cntS > 0:
      rm += 1
    cntS = max(0, cntS-1)
print(len(X)-rm*2)

Submission status: Accepted

Solution code:
X=input()
scount=0
ngcount=0
for x in X:
  if x=="S":
    scount+=1
  else:
    if scount>0:
      scount-=1
    else:
      ngcount+=1
print(ngcount*2)

Submission status: Accepted

Solution code:
def main():
    x=list(input())
    ans=cnt=key=0
    for i in range(len(x)):
        if key==0 and x[i]=="T":
            ans+=1
        elif x[i]=="S":
            key=1
            cnt+=1
        elif key==1 and x[i]=="T":
            cnt-=1
            if cnt==0:
                key=0
    ans+=cnt
    print(ans)
main()

Submission status: Accepted

Solution code:
X=input()
idx=0
motolenx=len(X)
lenx=len(X)
scount=0
while idx<motolenx:

    if X[idx]=="S":
        scount+=1
    if X[idx]=="T" and scount>0:
        scount-=1
        lenx-=2
    idx+=1


print(lenx)

Submission status: Accepted

Solution code:
sX = input()
s, t = 0, 0
for x in sX:
  if x == "S":
    s += 1
  elif s>0:
    s -= 1
  else:
    t += 1
print(s + t)

Submission status: Accepted

Solution code:
s = input()

mx = 0
l = len(s)
now = 0
for i in range(l):
    if s[i] == "T":
        now += 1
        mx = max(now,mx)
    else:
        now -= 1

print(mx*2)

Submission status: Accepted

Solution code:
X = input()
l = [''] * 2*10**5

last = X[0]
l[0] = X[0]
lastSindex = []
if last == 'S':
    lastSindex.append(0)

for i, a in enumerate(X):
    if i == 0:
        continue
    
    if a == 'T':
        if len(lastSindex) != 0:
            li = lastSindex.pop()
            l[li] = ''
        else:
            l[i] = a
    if a == 'S':
        lastSindex.append(i)
        l[i] = a

print(len(''.join(l)))

Submission status: Accepted

Solution code:
s = input()
a = list()
a.append('Q')

for i in range(len(s)):
    ch = s[i]
    if(ch == 'T' and a[len(a)-1]=='S'):
        a.pop()
    else:
        a.append(ch)
        
s = ""
s = s.join(a)

print(len(s)-1)

Submission status: Accepted

Solution code:
X=list(input())

ans=0
s=0
for x in X:
  if x=="T":
    if s==0:
      pass
    else:
      s-=1
      ans+=1
  else:
    s+=1
    
print(len(X)-2*ans)

Submission status: Accepted

Solution code:
s = input()
lens = len(s)
cnts = 0
ans = 0
for i in s:
  if i == 'S':
    cnts += 1
  elif i == 'T' and cnts > 0:
    ans += 1
    cnts -= 1
print(lens-ans*2)

Submission status: Accepted

Solution code:
X = input()

countS = 0
countT = 0
for x in X:
  if x == 'S':
    countS += 1
  if x == 'T' and countS > 0:
    countT += 1
    countS -= 1
print(len(X)-countT*2)

Submission status: Accepted

Solution code:
x=input()
length=len(x)
x=x+"E"
ch=x[0]
lists=[]
counter=1
for i in range(length):
    if x[i]==x[i+1]:
        counter+=1
    elif x[i]!=x[i+1] :
        lists.append(counter)
        counter=1
uselist=[]
if x[0]=="S" and x[-2]=="S":
    lists.append(0)
    for i in range(0,len(lists),2):
        uselist.append((lists[i],lists[i+1]))

elif x[0]=="S" and x[-2]=="T":
    for i in range(0,len(lists),2):
        uselist.append((lists[i],lists[i+1]))
   
elif x[0]=="T" and x[-2]=="S":
    lists.insert(0,0)
    lists.append(0)
    for i in range(0,len(lists),2):
        uselist.append((lists[i],lists[i+1]))

elif x[0]=="T" and x[-2]=="T":
    lists.insert(0,0)
    for i in range(0,len(lists),2):
        uselist.append((lists[i],lists[i+1]))
        
num=len(uselist)
uselist.append((0,0))
a=0
b=0
count=0
for i in range(num):
    a+=uselist[i][0]
    b+=uselist[i][1]
    if a<=b:
        count+=b-a
        a=0
        b=0
    if i==num-1 and a>b:
        count+=a-b
print(count)

Submission status: Accepted

Solution code:
S = input()
ans = 0
pos = 0
scount = 0

while pos < len(S):

    while pos <len(S) and S[pos] == 'T':
        pos += 1
        ans += 1
    if pos >= len(S):
        break

    while pos <len(S) and S[pos] == 'S':
        pos += 1
        scount += 1
    if pos >= len(S):
        ans += scount
        break

    tcount = 0
    while pos < len(S) and S[pos] == 'T':
        pos += 1
        tcount += 1
    if pos >= len(S):
        ans += abs(scount-tcount)
        break
      
    if scount >= tcount:
        scount = scount -tcount
    else: #scount < tcount
        ans += tcount-scount
        scount = 0

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)

def main():
    s = input().strip()
    stack = []
    for c in s:
        if c == 'S':
            stack.append('S')
        else:
            if len(stack) > 0 and stack[-1] == 'S':
                stack.pop()
            else:
                stack.append('T')
    print(len(stack))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
X = input()

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque

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

for i in X[1:]:
    if stack:
        if stack[-1] == 'S' and i == 'T':
            stack.pop()
        else:
            stack.append(i)
    else:
        stack.append(i)

print(len(stack))

Submission status: Accepted

Solution code:
X=input()
k=0 # これまでに現れたSの数-Tの数 ただし0が下限
cnt=0 # k=0のときに現れたTの数
for c in X:
    if c=='S':
        k+=1
    else:
        if k==0:
            cnt+=1
        else:
            k-=1
print(cnt*2)

Submission status: Accepted

Solution code:
x = input()

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

Submission status: Accepted

Solution code:
w=input()
count=0
counts=0
for i in range(len(w)):
  if w[i]=="S":
    counts+=1
  else:
    if counts!=0:
      counts-=1
      count+=1
print(len(w)-count*2)

Submission status: Accepted

Solution code:
stack = 0
cnt = 0
for x in input():
    if x == "S": stack += 1
    else:
        if stack: stack -= 1
        else: cnt += 1
print(cnt + stack)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- Coding: utf-8 -*-

import sys


def main():
    X = input()
    t = 0
    s = 0

    for x in X:
        if x == 'T':
            if s == 0:
                t += 1
            else:
                s -= 1
        else:
            s += 1

    print(t+s)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
x=str(input())
c=0
ans=0
for i in x:
    if i=='S':
        c+=1
    if c>0 and i=='T':
            c-=1
            ans+=2
#print(len(x),ans)
print(len(x)-ans)

Submission status: Accepted

Solution code:
def slove():
    import sys
    input = sys.stdin.readline
    x = str(input().rstrip('\n'))
    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)


if __name__ == '__main__':
    slove()

Submission status: Accepted

Solution code:
X=list(input())
N=len(X)
count=0

for x in X:
    if x=="S":
        count+=1
    else:
        if count>0:
            count-=1
            N-=2
print(N)

Submission status: Accepted

Solution code:
#from collections import defaultdict


def main():
    ans=0
    tmp=0
    x=input()
    for i in x:
        if tmp==0 and i=="T":
            ans+=1
        elif i=="T":
            tmp-=1
        else:
            tmp+=1
    print(ans*2)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
S=input()

ans=len(S)
n=0
for s in S:
  if s=='S':
    n+=1
  elif s=='T' and n>0:
    n-=1
    ans-=2
print(ans)

Submission status: Accepted

Solution code:
x = input()
n = len(x)
res = 0
tres = 0
for c in x:
    if c == 'T':
        tres += 1
    else:
        tres -= 1
    res = max(tres,res)

print(res * 2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
S = input()
Stack = []
for i in range(len(S)):
  if S[i] == "S":
    Stack.append("S")
  elif S[i] == "T" and len(Stack) == 0:
    Stack.append("T")
  elif S[i] == "T" and Stack[-1] == "T":
    Stack.append("T")
  elif S[i] == "T" and Stack[-1] == "S":
    Stack.pop()
print(len(Stack))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
S=input()

tmp=0
ans=len(S)

for s in S:
  if s=="S":
    tmp+=1
  if s=="T" and tmp>0:
    ans-=2
    tmp-=1

print(ans)

Submission status: Accepted

Solution code:
S=input()
A1=[]
L=len(S)
A1.append(S[0])

for i in range(1,L):
    if len(A1)>=1 and A1[-1]=="S" and S[i]=="T":
        A1.pop(-1)
    else:
        A1.append(S[i])

print(len(A1))

Submission status: Accepted

Solution code:
A2=list(input())
A1=[]
L=len(A2)
A1.append(A2[0])

for i in range(1,L):
    if len(A1)>=1 and A1[-1]=="S" and A2[i]=="T":
        A1.pop(-1)
    else:
        A1.append(A2[i])

print(len(A1))

Submission status: Accepted

Solution code:

def main():
    s = list(input())
    l = ["b"]
    for i in range(len(s)):
        if s[i] == "S":
            l.append("S")
        elif s[i] == "T" and (l[-1] == "T" or len(l) == 1):
            l.append("T")
        elif s[i] == "T" and l[-1] == "S":
            l.pop()
    print(len(l) - 1)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

def main():
    x = input().strip() + "x"
    c = []
    pre = x[0]
    count = 1
    for i in range(1, len(x)):
        if x[i] != pre:
            c.append(count)
            count = 1
        else:
            count += 1
        pre = x[i]

    ans = len(x) - 1
    stock = 0
    if x[0] == "S":
        start = 1
    else:
        start = 2

    for i in range(start, len(c), 2):
        if c[i-1] + stock <= c[i]:            
            ans -= (c[i-1]+stock) * 2
            stock = 0
        else:
            ans -= c[i] * 2
            stock += c[i-1] - c[i]
    
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
X = input()
s = 0
t = 0
for x in X:
    if x == 'S':
        s += 1
    else:
        if s > 0:
            s -= 1
        else:
            t += 1
print(s+t)

Submission status: Accepted

Solution code:
t=m=0
for c in input():t+=2if c=="T"else-2;m=max(m,t)
print(m)

Submission status: Accepted

Solution code:
def resolve():
    x = input()
    ans = [x[0]]
    for c in x[1:]:
        if ans and ans[-1] == "S" and c == "T":
            ans.pop()
        else:
            ans.append(c)
    print(len(ans))


if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
def a_st_ring():
    X = input()
    stack = []
    for c in X:
        if len(stack) > 0 and stack[-1] == 'S' and c == 'T':
            stack.pop()
        else:
            stack.append(c)
    return len(stack)

print(a_st_ring())

Submission status: Accepted

Solution code:
a = []
x = input()
for i in x:
    if i == "T":
        if len(a) == 0 or a[-1] == "t":
            a.append("t")
        else:
            del a[-1]
    else:
        a.append("s")
print(len(a))

Submission status: Accepted

Solution code:
x = input()
cur = x[0]
cnt = 0
rle = []
for i in x:
    if i == cur:
        cnt += 1
    else:
        rle.append([cur, cnt])
        cur = i
        cnt = 1
rle.append([cur, cnt])

n = len(rle)
ans = 0
for i, (cur, cnt) in enumerate(rle):
    if cur == "T":
        continue
    if i+1 < n:
        nxt, nxt_cnt = rle[i+1]
        if cnt <= nxt_cnt:
            ans += cnt
        else:
            ans += nxt_cnt
            if i+2 < n:
                rle[i+2][1] += cnt-nxt_cnt
print(len(x) - 2*ans)

Submission status: Accepted

Solution code:
X = input()
rs = []
n = 0
for c in X:
  if c == "T":
    n += 1
  if c == "S":
    n -= 1
  rs.append(n)
r = max(rs)*2
print(r)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict
sys.setrecursionlimit(200000)
input = sys.stdin.readline

# N, = map(int, input().split())
S = list(input().strip())

s = 0
t = 0
for i in range(len(S)):
    if S[i] == "S":
        s += 1
    else:
        if s > 0:
            s -= 1
        else:
            t += 1

print(s + t)

Submission status: Accepted

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

def main():
    X = input().strip()
    A = []
    for c in X:
        if len(A) > 0 and A[-1] == "S" and c == "T":
            A.pop()
        else:
            A.append(c)
    print(len(A))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s=input()
scnt=0;tcnt=0
for i in s:
  if i=="T":
    if scnt!=0:
      scnt-=1
    else:tcnt+=1
  elif i=="S":
    scnt+=1
print(scnt+tcnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
X = input()
s = cnt = 0

for i in range(len(X)):
    if X[i] == 'S':
        s += 1
    elif s != 0:
        cnt += 2
        s -= 1

ans = len(X) - cnt
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x = input()

s=t=k=0

for i in range(len(x)):
  if x[i] =='S':
    s+=1
  elif s!=0:
    k+=2
    s-=1
    
print(len(x) -k)

Submission status: Accepted

Solution code:
x=list(input())
ans=0
check=0
for i in x:
	if i=='T':
		if check==0:
			ans+=1
		check-=1
		check=max(check,0)

	else:
		check+=1
print(ans*2)

Submission status: Accepted

Solution code:
x=input()
s=t=k=0
for i in range(len(x)):
	if x[i]=="S":
		s+=1
	elif s!=0:
		k+=2
		s-=1
print(len(x)-k)

Submission status: Accepted

Solution code:
x=list(input())
from collections import deque
l=deque()
for a in x:
    if a=="S":
        l.append(a)
    else:
        if not l or l[-1]=="T":
            l.append(a)
        else:
            d=l.pop()
print(len(l))

Submission status: Accepted

Solution code:
X = input()
cnt_s = 0
ans_t = 0
for x in X:
    if x == "S":
        cnt_s += 1
    else:
        if cnt_s > 0:
            cnt_s -= 1
        else:
            ans_t += 1
print(ans_t*2)

Submission status: Accepted

Solution code:
c = 0
cs = 0
s = input()
for i in s:
  if i == 'S':
    cs += 1
  else:
    if cs > 0:
      cs -= 1
      c += 1
print(len(s)-2*c)

Submission status: Accepted

Solution code:
X = list(input())
c = 0
for x in X:
    if x=='S':
        c += 1
    else:
        if c > 0:
            c -= 1
print(2*c)

Submission status: Accepted

Solution code:
x = input()

ans = ""
for i in x:
  ans += i
  if len(ans) >= 2 and (ans[-2] == "S" and ans[-1] == "T"):
    ans = ans[:-2]

print(len(ans))

Submission status: Accepted

Solution code:
X = input()
Y = []
for i in X:
    Y.append(i)
    if len(Y) > 1:
        if Y[-2] + Y[-1] == "ST":
            Y.pop()
            Y.pop()
print(len(Y))

Submission status: Accepted

Solution code:
def main():
    S = input()
    stack = []
    for i in range(len(S)):
        stack.append(S[i])
        while len(stack) > 1 and "".join(stack[-2:]) == "ST":
            stack.pop()
            stack.pop()
        # print(stack)
    return len(stack)


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