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 pysnooper
#import os,re,sys,operator,math,heapq,string
from collections import Counter,deque
#from operator import itemgetter
#from itertools import accumulate,combinations,groupby,combinations_with_replacement
from sys import stdin,setrecursionlimit
#from copy import deepcopy

setrecursionlimit(10**6)
input=stdin.readline

s=input().rstrip()
q=deque()
for i in s:
    if i=="S":
        q.append(i)
    else:
        if len(q)==0:
            q.append(i)
        elif q[-1]=="T":
            q.append(i)
        else:
            q.pop()
   # print(q)
print(len(q))

Submission status: Accepted

Solution code:
x=input()
s,t=0,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:
# -*- coding: utf-8 -*-
#AGC005A
import sys

#n= int(input())
#tmp = input().split()
#n,k = list(map(lambda a: int(a), tmp))
x = input()

x_len=len(x)

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

Submission status: Accepted

Solution code:
x = input()
t = 0
c = 0
for i in range(len(x)):
    if x[-i-1] == "T":
        t += 1
    else:
        r = 1 if t > 0 else 0
        t -= r
        c += r
print(len(x) - c * 2)

Submission status: Accepted

Solution code:
s = input()

del_list = [] 
l = 0
pointer = 0
for i,v in enumerate(s):
    if v == "S":
        del_list.append([i,-1])
        l += 1
    else:
        if l != 0 and pointer != l:
            del_list[pointer][1]  = i
            pointer += 1


rems = 0
delpair = 0
for i in del_list:
    if i[1] == -1:
        rems += 1
    else:
        delpair += 1

remt = s.count("T") - delpair
print(remt+rems)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, math, fractions, itertools, collections, copy, heapq


def solve(X: str):
    stack = []
    for s in X:
        if s == 'S':
            stack.append(s)
        if s == 'T':
            if len(stack) == 0:
                stack.append(s)
            elif stack[-1] == 'T':
                stack.append(s)
            elif stack[-1] == 'S':
                stack.pop()
    print(len(stack))
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    X = next(tokens)  # type: str
    solve(X)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def sol():
    X = input()

    scount = 0
    ans = 0
    for x in X:
        if x == 'S':
            scount += 1
        else:
            if scount == 0:
                ans += 1
            else:
                scount -= 1

    print(ans * 2)

sol()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x = input()

s = []
for i in range(len(x)):
    if x[i] == "S":
        s.append(x[i])
    else:
        if len(s) != 0 and s[-1] == "S":
            s.pop(-1)
        else:
            s.append(x[i])

print(len(s))

Submission status: Accepted

Solution code:
a=b=0
for i in input():c=i=="T";d=a>0;a+=1-c-c*d;b+=c*(1-d)
print(a+b)

Submission status: Accepted

Solution code:
# 解答例
# スタック

x=input()
n=len(x)
stack=[]

for i in range(n):
    if x[i]==('S'):
        stack.append(x[i])
    else:
        if stack and stack[-1]==('S'):
            stack.pop()
        else:
            stack.append(x[i])
print(len(stack))

Submission status: Accepted

Solution code:
# 2019/08/15

x=input()
n=len(x)
ans=n
cnt=0

for i in range(n):
    if x[i]==('S'):
        cnt+=1
    else:
        if cnt>0:
            cnt-=1
            ans-=2
print(ans)

Submission status: Accepted

Solution code:
X = input()
cnt_s = 0
cnt = 0
for s in X:
    if(s == 'S'):
        cnt_s += 1
    else:
        if(cnt_s>0):
            cnt += 2
            cnt_s -= 1
print(len(X)-cnt)

Submission status: Accepted

Solution code:
s = input()
tt = 0
cnt = 0
for i in range(len(s)):
    if s[-i-1] == "T":
        tt += 1
    elif s[-i-1] == "S":
        t = min(1, tt)
        cnt += t
        tt -= t
print(len(s) - cnt * 2)

Submission status: Accepted

Solution code:
S = input()
ans = 0
cnt = 0
for s in S:
    if s == "T":
        if cnt == 0:
            ans += 1
        else:
            cnt -= 1
    else:
        cnt += 1
print(ans*2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#AGC5 A

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

stack = []
x = input()
n = len(x)
for i in range(n):
    if x[i] == 'S':
        stack.append(x[i])
    else:
        if len(stack) == 0 or stack[-1] != 'S':
            stack.append(x[i])
        elif stack[-1] == 'S':
            stack.pop()
print(len(stack))

Submission status: Accepted

Solution code:
x = input()
ans = 0

count = 0
for i in range(len(x)):
    if x[i] == "S":
        count += 1
    else:
        count -= 1
    if count < 0:
        ans+=1
        count = 0
count = 0
for i in range(len(x)-1,-1,-1):
    if x[i] == "T":
        count += 1
    else:
        count -= 1
    if count <0 :
        ans+=1
        count = 0
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
l=[]
x=list(input())
for i in x:
  if i=='S':
    l.append(i)
  elif len(l)>0:
    a=l.pop()
    if a=='S':
      pass
    else:
      l.append('T')
      l.append('T')
  else:
    l.append(i)
print(len(l))

Submission status: Accepted

Solution code:
X = list(input())

count_S = 0
count_T = 0
TS_max = 0

for i in range(len(X)):
    if X[i] == 'S':
        count_S += 1
    elif X[i] == 'T':
        count_T += 1
    tmp = count_T - count_S
    TS_max = max(TS_max, tmp)

ans = TS_max * 2
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:


x=input()
sbuf = 0
tot = len(x)
for c in x:
    if c == "S":
        sbuf += 1
    else:
        if sbuf > 0:
            sbuf -= 1
            tot-=2

print(tot)

Submission status: Accepted

Solution code:
a = input()
t = 0
cnt = 0
for i in range(len(a)-1, -1, -1):
    if a[i] == "T":
        t += 1
    elif a[i] == "S" and t != 0:
        t -= 1
        cnt += 1
print(len(a) - cnt * 2)

Submission status: Accepted

Solution code:
s = input()
cnt, ans = 0, 0
for x in s:
    if x == 'S':
        cnt += 1
    else:
        if cnt > 0:
            cnt -= 1
            ans += 1
print(len(s)-ans*2)

Submission status: Accepted

Solution code:
from collections import deque

S = input()
lenS = len(S)
pops = 0
ans = lenS
stack = deque()
for i in range(lenS):
    if stack and S[i] == "T":
        a = stack.pop()
        if a == "S":
            ans -= 2
    else:
        stack.append(S[i])
print(str(ans))

Submission status: Accepted

Solution code:
X = input()

cnt1 = 0
cnt2 = 0
cnt = 0
for i in range(len(X)):
    if X[i] == 'S':
        if cnt2:
            cnt += cnt2 * 2
            cnt2 = 0
        cnt1 += 1
    elif cnt1 and X[i] == 'T':
        cnt2 += 1
        cnt1 -= 1

if cnt2:
    cnt += cnt2 * 2

print(len(X) - cnt)

Submission status: Accepted

Solution code:
# |X|<=2*10**5
# 除去回数は最大10**5
# 10**5 < 10**10000 -> STの繰り返しだったとしても10**10000回に到達することはない

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

Submission status: Accepted

Solution code:
X=input()
ans=0
memo=0
for i in range(len(X)):
    if X[i]=="T":
        if memo==0:pass
        else:memo-=1
    else:memo+=1
print(memo*2)

Submission status: Accepted

Solution code:
def main():
    ls = list(input())
    t = 0
    cnt = 0
    for i in reversed(range(len(ls))):
        if ls[i]=="T":
            t +=1
        if ls[i]=="S":
            if t==0:
                pass
            else:
                cnt +=2
                t -= 1
    print(len(ls)-cnt)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
x = list(input())
count = 0
stock_s = 0

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

Submission status: Accepted

Solution code:
import math
import copy
from operator import mul
from functools import reduce
from collections import defaultdict
from collections import Counter
from collections import deque
# 直積 A={a, b, c}, B={d, e}:のとき，A×B={(a,d),(a,e),(b,d),(b,e),(c,d),(c,e)}: product(A, B)
from itertools import product
# 階乗 P!: permutations(seq), 順列 {}_len(seq) P_n: permutations(seq, n)
from itertools import permutations
# 組み合わせ {}_len(seq) C_n: combinations(seq, n)
from itertools import combinations
# 一次元累積和
from itertools import accumulate
from bisect import bisect_left, bisect_right

import re
# import numpy as np
# from scipy.misc import perm
# from scipy.misc import comb

def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W

# 四方向: 右, 下, 左, 上
dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]
 
def i_inpl(): return int(input())
def l_inpl(): return list(map(int, input().split()))
def line_inpl(x): return [i_inpl() for _ in range(x)]

INF = int(1e18)
MOD = int(1e9)+7 # 10^9 + 7

########

X = input()

t_cnt, s_cnt = 0, 0
for Xi in X:
    if Xi == "S":
        s_cnt += 1
    elif Xi == "T":
        if s_cnt > 0:
            s_cnt -= 1
        else:
            t_cnt += 1

print(t_cnt + s_cnt)

Submission status: Accepted

Solution code:
# AGC005A - STring
def main():
    X = input().rstrip()
    ans, p = 0, 0  # pair
    for i in X:
        if i == "S":
            p += 1
        else:
            if p:
                p -= 1
            else:
                ans += 1  # remaining T
    ans += p  # add remaining S
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# AGC005A - STring
def main():
    X = input().rstrip()
    ans, s, t = 0, 0, 0
    for i in X:
        if i == "S":
            s += 1
        else:
            if t < s:
                t += 1
            else:
                ans += 1
    ans += s - t
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
X = list(input())
S = 0
T = 0
for i in X:
    if i == 'T':
        if S > 0:
            S -= 1
        else:
            T += 1
    if i == 'S':
        S += 1

print (S + T)

Submission status: Accepted

Solution code:
def solve(x):
    n = len(x)
    f, s = 0, 0
    for c in x:
        if c == "S":
            f += 1
        elif f > 0:
            f -= 1
            s += 2
    return n - s

x = input()
print(solve(x))

Submission status: Accepted

Solution code:
X = input()
ans = 0
S_count = 0
for i in range(len(X)):
    if X[i] == 'S':
        S_count -= 1
    elif S_count == 0 and X[i] == 'T':
        ans += 1
    elif X[i] == 'T':
        S_count += 1

print(2*ans)

Submission status: Accepted

Solution code:
def main():
    X = input()
    ans = len(X)
    s = 0
    for c in X:
        if c == "S":
            s += 1
        elif c == "T":
            if s:
                s -= 1
                ans -= 2
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:

def main():
    data  = list(input())

    ans = len(data)
    count_s = 0
    for i in range(len(data)):
        if data[i] == 'S':
            count_s += 1
        else:
            if count_s > 0:
                count_s -= 1
                ans -= 2

    print(ans)




if __name__ == '__main__':
    main()

Submission status: Accepted

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

print(s+t)

Submission status: Accepted

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

c = 0
uppun = 0
for i, x in enumerate(X):
    if x == 'S':
        uppun += 1
    if x == 'T':
        c += 1
        if uppun > 0:
            uppun -= 1
    if c == N//2:
        ans = N - (i+1-c - uppun)*2
        break

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s=input()
ss=0
tt=0
for i in range(len(s)):
    if s[i]=="S":
        ss+=1
    if s[i]=="T" and tt<ss:
        tt+=1
print(len(s)-2*tt)

Submission status: Accepted

Solution code:
X = input()

count = 0
count_s = 0
i = 0
while i < len(X):
    if i < len(X) - 1 and X[i] == 'S' and X[i+1] == 'T':
        count += 1
        i += 1
    elif X[i] == 'S':
        count_s += 1
    elif X[i] == 'T' and count_s > 0:
        count_s -= 1
        count += 1
    else:
        count_s = 0
    i += 1

count = min(count, 10 ** 10000)
print(len(X) - count * 2)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

X = list(input())
num = len(X)
stack = []

for i in X:
    if i == "S":
        stack.append(i)
    else:
        if stack == [] or stack[-1] == "T":
            stack.append(i)
        else:
            stack.pop()
print(len(stack))

Submission status: Accepted

Solution code:
X=input()

s_cnt=0;t_cnt=0
for i in range(len(X)):
    if X[i] == "S":
        s_cnt+=1
    else:
        if s_cnt>0:
            s_cnt-=1
            t_cnt+=1

print(len(X)-t_cnt*2)

Submission status: Accepted

Solution code:
s = input()
cnt = 0
cnt_set = 0
for x in s:
    if x == 'S':
        cnt += 1
    else:
        if cnt > 0:
            cnt_set += 1
            cnt -= 1
print(len(s)-2*cnt_set)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
S = input()
scount = 0
tcount = 0
ans = 0
l = len(S)
for c in S:
    if c == "S":
        scount += 1
    else:
        if scount >0:
            scount -=1
            ans += 2

ans += min (scount,tcount) * 2
ans = len(S) -ans
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
X = input()

ans = 0
s = 0

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

print(len(X) - ans)

Submission status: Accepted

Solution code:
from fractions import gcd
import math
def inverse(a):
    return pow(a, mod - 2, mod)


def usearch(x, a):
    lft = 0
    rgt = len(a) + 1
    while rgt - lft > 1:
        mid = (rgt + lft) // 2
        if a[mid] <= x:
            lft = mid
        else:
            rgt = mid
    return lft



def main():
    x =input()
    n = len(x)
    s = 0
    ans = 0
    for i in range(n):
        if x[i] == 'S':
            s +=1
        elif s > 0:
            s -= 1
            ans += 1
    print(n-ans*2)

main()

Submission status: Accepted

Solution code:
# SはストックのS 存在するぶんだけTと相殺できる
# 消した数じゃなくて残った数を数えるってのを読み飛ばしてて1WA
# 変数ミスで1WA

x = input()
stock = 0
count = 0
for a in x:
    if a == "S":
        stock += 1
    else:
        if stock > 0:
            count += 2
            stock -= 1
print(len(x)-count)

Submission status: Accepted

Solution code:
X = input()

ans = ["foo"]
for i in range(len(X)):
    ans.append(X[i])
    if ans[-2] == "S" and ans[-1] == "T":
        ans.pop()
        ans.pop()

print(len(ans) - 1)

Submission status: Accepted

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

Submission status: Accepted

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

print(s+t)

Submission status: Accepted

Solution code:
x=input()
l=len(x)
t=0
for i in range(l):
    if x[i]=="T":
        t+=1
    else:
        break
s=0
for j in range(l):
    if x[l-1-j]=="S":
        s+=1
    else:
        break
x=x[t:l-s]
l=len(x)
if l==0:
    print(s+t)
else:
    ans=l
    bef=x[0]
    cha=1
    for k in range(1,l):
        if bef=="S":
            if x[k]=="S":
                cha+=1
            else:
                bef=x[k]
                cha-=1
                ans-=2
        else:
            if x[k]=="S":
                bef=x[k]
                cha+=1
            else:
                if cha>0:
                    cha-=1
                    ans-=2
    print(ans+s+t)

Submission status: Accepted

Solution code:
S = input()
cs = 0
ans = 0
for i in range(len(S)):
  si = S[i]
  if si == 'T' and cs > 0:
    cs -= 1
    ans += 2
  elif si == 'S':
    cs += 1
    
print(len(S)-ans)

Submission status: Accepted

Solution code:
X = input()
N = len(X)
tmpS = 0
tmpans = 0

for i in range(N):
    if X[i] == 'S':
        tmpS += 1
    else:
        if tmpS == 0:
            tmpans += 1
        else:
            tmpS -= 1

print(tmpans*2)

Submission status: Accepted

Solution code:
import math
import copy
from operator import mul
from functools import reduce
from collections import defaultdict
from collections import Counter
from collections import deque
# 直積 A={a, b, c}, B={d, e}:のとき，A×B={(a,d),(a,e),(b,d),(b,e),(c,d),(c,e)}: product(A, B)
from itertools import product
# 階乗 P!: permutations(seq), 順列 {}_len(seq) P_n: permutations(seq, n)
from itertools import permutations
# 組み合わせ {}_len(seq) C_n: combinations(seq, n)
from itertools import combinations
# 一次元累積和
from itertools import accumulate
from bisect import bisect_left, bisect_right

# import numpy as np
# from scipy.special import perm
# from scipy.special import comb

def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W

# 四方向: 右, 下, 左, 上
dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]
 
def i_inpl(): return int(input())
def l_inpl(): return list(map(int, input().split()))
INF = float("inf")

########
X = input()

cnt = 0
sum_cnt = 0
for xi in X:
    if xi == "S":
        cnt += 1
    else:
        if cnt > 0:
            cnt -= 1
            sum_cnt += 1

print(len(X) - sum_cnt*2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
X = input()

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

Submission status: Accepted

Solution code:
from sys import stdin
from collections import deque
X = (stdin.readline().rstrip())
stack = []
for i in X:
    if i == "S":
        stack.append(i)
    elif (i == "T" and stack == []):
        stack.append(i)
    elif (i == "T" and stack[-1] == "S"):
        stack.pop()
    else:
        stack.append(i)
print(len(stack))

Submission status: Accepted

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

snum = 0
stnum = 0
for item in x:
    if item == "S":
        snum += 1
    if item == "T":
        if snum > 0:
            snum -= 1
            stnum += 1
print(len(x) - 2*stnum)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
A - STring
https://atcoder.jp/contests/agc005/tasks/agc005_a

"""
import sys


def solve(X):
    stat = 0
    ans = 0
    for x in X:
        if x == 'T':
            stat += 1
            ans = max(ans, stat*2)
        else:
            stat -= 1
    return ans


def main(args):
    X = input()
    ans = solve(X)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
string = input()
num_t = 0
num_s = 0
for char in string:
    if char == "S":
        num_s += 1
    elif num_s > 0:
        num_s -= 1
    else:
        num_t += 1
print(num_t + num_s)

Submission status: Accepted

Solution code:
X = input()

count, ls = 0, 0
for x in X:
	if x == "S":
		ls += 1
	else:
		if 0 < ls:
			ls -= 1
		else:
			count += 1

print(ls + count)

Submission status: Accepted

Solution code:
X = input()

S_start_index = 0
for i in range(int(len(X)/2)):
    if X[i] == "S":
        S_start_index = i
        break

T_stop_index = 0
#print(len(X)-1 > int(len(X)/2)-1)
for i in range(len(X)-1, int(len(X)/2)-1, -1):
#    print("i=", i)
    if X[i] == "T":
        T_stop_index = i + 1
        break


#print(S_start_index, T_stop_index)

count = 0
delate_count = 0
max_count = 0
for i in range(S_start_index ,T_stop_index):
    if X[i] == "S":
        count += 1
    else:
        if count == 0:
            pass
        else:
            count -= 1
            delate_count += 1


#print(delate_count)
output = len(X) - delate_count*2

print(output)

Submission status: Accepted

Solution code:
X = input()
ans = 0
c = 0
for i in X:
    if i == 'T':
        if c == 0:
            ans += 2
        else:
            c -= 1
    else:
        c += 1
print(ans)

Submission status: Accepted

Solution code:
x = input()
t = 0
s = 0
cnt = 0
i=0
while t<len(x)//2:
    if x[i]=='S':
        s+=1
    if x[i]=='T':
        if s>0:
            cnt+=2
            s-=1
        t+=1
    i+=1
print(len(x)-cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s=input()

ans=0
q=0
for z in s:
  if z=="S":
    q+=1
  else:
    if q!=0:
      q-=1
      ans+=2

print(len(s)-ans)

Submission status: Accepted

Solution code:
s=input()
q=[]
cnt=0
for i in s:
    if i=='S':
        q.append('S')
    else:  # t
        if q:
            q.pop()
            cnt+=2
print(len(s)-cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
S = list(input())
N = len(S)
A = [0]*N

d = dict()
d['T'] = 1
d['S'] = -1

for i in range(N):
    A[i] = d[S[i]] + A[i-1]
        
print(max(A)*2)

Submission status: Accepted

Solution code:
s = input()
array = []

for char in s:
    if char == "S":
        array.append(char)
    elif char == "T":
        if len(array) == 0:
            array.append(char)
        elif array[-1] == "S":
            array.pop()
        else:
            array.append(char)
print(len(array))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
X = input()

stack = []
for x in X:
    if len(stack) != 0 and x == "T" and stack[-1] == "S":
        stack.pop()
    else:
        stack.append(x)

print(len(stack))

Submission status: Accepted

Solution code:

def main():
    buf = input()
    X = buf
    length = len(X)
    S_count = 0
    for i in X:
        if i == 'S':
            S_count += 1
        else: # i == 'T'
            if S_count > 0:
                S_count -= 1
                length -= 2
    print(length)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s=input()
stack=[]
ans=0
for i in s:
    if i=="S":
        stack.append(1)
    elif len(stack)>0 and i=="T":
        stack.pop()
        ans+=1
print(len(s)-ans*2)

Submission status: Accepted

Solution code:
s = input()

a = 0
b = 0

for i in range(len(s)):
    if s[i]=='S':
        a+=1
    elif a>0:
        b+=2
        a-=1
print(len(s)-b)

Submission status: Accepted

Solution code:
from collections import deque

X = input()

dq = deque()

for x in X:
    if not dq:
        dq.append(x)
    elif dq[-1] == "S" and x == "T":
        dq.pop()
    else:
        dq.append(x)

ans = len(dq)
print(ans)

Submission status: Accepted

Solution code:
X=input()
count=0
balance=0
for x in X:
  if balance==0 and x=="T":
    count+=1
  elif x=="S":
    balance-=1
  elif x=="T":
    balance+=1
X=X[::-1]
balance=0
for x in X:
  if balance==0 and x=="S":
    count+=1
  elif x=="T":
    balance-=1
  elif x=="S":
    balance+=1
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections

s=input()
n=len(s)
slen=collections.deque()
tlen=collections.deque()
cnt=0
ans=0
for i in range(n):
  if s[i]=='S':
    tpos=i
    break
else:
  tpos=n
for i in range(n-1,-1,-1):
  if s[i]=='T':
    spos=i
    break
else:
  spos=-1
s=s[tpos:spos+1]+'x'
for i in range(len(s)-1):
  cnt+=1
  if s[i]!=s[i+1]:
    if s[i]=='S':
      slen.append(cnt)
      cnt=0
    else:
      tlen.append(cnt)
      cnt=0
for i in range(len(slen)):
  stmp,ttmp=slen.popleft(),tlen.popleft()
  if stmp>ttmp:
    ans+=ttmp
    if len(slen)>0:
      tmp=slen.popleft()+(stmp-ttmp)
      slen.appendleft(tmp)
  else:
    ans+=stmp
print(n-ans*2)

Submission status: Accepted

Solution code:
s = input()
ans = len(s)
cnt = 0
for c in s:
  if c == 'S':
    cnt += 1
  elif cnt > 0:
    ans -= 2
    cnt -= 1
print(ans)

Submission status: Accepted

Solution code:
S = input()
N = len(S)
t = 0
num = 0
for i in range(N):
    if S[i] == 'S':
        t += 1
    else:
        if t > 0:
            num += 2
            t -= 1

print(N - num)

Submission status: Accepted

Solution code:
s = input()

s_cnt, t_cnt = 0, 0
t = 0
for i in s:
    if i == "S":
        s_cnt += 1
    if s_cnt == 0 and i == "T":
        t += 1
    if s_cnt != 0 and i == "T":
        s_cnt -= 1
    s_cnt = max(s_cnt, 0)

print(2*t)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s = input()
ans = 0
temp = 0
for i in range(len(s)):
  if s[i] == 'T':
    temp -= 1
  else:
    temp += 1
  ans = min(temp,ans)
print(ans*(-2))

Submission status: Accepted

Solution code:
s = input()
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(T * 2)

Submission status: Accepted

Solution code:
s = input()
cnt_s = 0
cnt_t = 0
for i in s:
    if i == 'S':
        cnt_s += 1
    elif cnt_s > 0:
        cnt_s -= 1
    else:
        cnt_t += 1

print(cnt_s + cnt_t)

Submission status: Accepted

Solution code:
from collections import deque

X = input()

dq = deque()

for x in X:
    if not dq:
        dq.append(x)
    elif dq[-1] == "S" and x == "T":
        dq.pop()
    else:
        dq.append(x)
    
ans = len(dq)
print(ans)

Submission status: Accepted

Solution code:
s = input()
y = [0]
z = []
for t in s:
    if t == "S":
        y.append(t)
    elif y[-1] == "S":
        del y[-1]
    else:
        z.append(t)

print(len(y) + len(z) - 1)

Submission status: Accepted

Solution code:
from collections import deque


X = input()
stk = deque()

for c in X:
    if len(stk) == 0:
        stk.append(c)
    else:
        if stk[-1] == "S":
            if c == "T":
                stk.pop()
            else:
                stk.append(c)
        else:
            stk.append(c)
print(len(stk))

Submission status: Accepted

Solution code:
X = input()
cnt_S, cnt_T = 0, 0

for c in X:
    if c == "S":
        cnt_S += 1
    else:
        if cnt_S:
            cnt_S -= 1
        else:
            cnt_T += 1
print(cnt_S + cnt_T)

Submission status: Accepted

Solution code:
X = input()

s_stack=0
t_stack=0

for x in X:
  if x=='S':
    s_stack +=1
  if x=='T':
    if s_stack:
      s_stack -=1
    else:
      t_stack +=1

answer = s_stack + t_stack
print(answer)

Submission status: Accepted

Solution code:
x = input().strip()
stack = []

for _x in x:
  if stack and _x == 'T' and stack[-1] == 'S':
    stack.pop()
  else:
    stack.append(_x)
print(len(stack))

Submission status: Accepted

Solution code:
x=input();a,l=0,len(x)
for i in range(l):
  if x[l-i-1]=="T":a+=1
  else:a=max(0,a-1)
print(a*2)

Submission status: Accepted

Solution code:
X = input()

count = 0
s = 0
for x in X:
    if x == 'S':
        s += 1
    elif s > 0:
        s -= 1
        count += 1

print(len(X) - count*2)

Submission status: Accepted

Solution code:
s=input()
ls=len(s)
c=0
ans=0
for i in range(ls):
    if s[i]=='S':
        c+=1
    elif c>0:
        ans+=2
        c-=1
print(ls-ans)

Submission status: Accepted

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

Submission status: Accepted

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