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:
s = input()
s_cnt =0
t_cnt =0
cnt = 0

for i in range(len(s)):
    if s[i] == 'S':
        s_cnt += 1
    if s[i] == 'T':
        t_cnt += 1
        if s_cnt >0:
            t_cnt -= 1
            s_cnt -= 1
print(t_cnt + s_cnt)

Submission status: Accepted

Solution code:
X = input()

cache = 0
ans = len(X)

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

print(ans)

Submission status: Accepted

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
from math import sqrt
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

#############
# Main Code #
#############

S = input()
N = len(S)

s_cnt = 0
ans = 0
for i in range(N):
    if S[i] == 'S':
        s_cnt += 1
    else:
        if s_cnt == 0:
            ans += 1
        else:
            s_cnt -= 1
print(s_cnt + ans)

Submission status: Accepted

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

print(len(X) - 2*ans)

Submission status: Accepted

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
s = input()
n = len(s)
ans = []
for i in range(n):
    if s[i] == "S":
        ans.append(s[i])
    else:
        if ans == [] or ans[-1] != "S":
            ans.append(s[i])
        else:
            ans.pop()
print(len(ans))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
x=input()
n=len(x)
s=0
for i in x:
  if s and i=='T':s-=1
  if i=='S':s+=1
print(2*s)

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    s = input()
    ans = len(s)
    cnt = 0

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    X = input()
    S_COUNT = 0
    delete = 0
    for c in X:
        if c == "S":
            S_COUNT += 1
        else:
            if S_COUNT:
                delete += 2
                S_COUNT -= 1
                
    print(len(X)-delete)
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque
X = input()
que = deque()
for x in X:
    if x=='S':
        que.append('S')
    elif x=='T' and (not que or que[-1]=='T'):
        que.append('T')
    elif x=='T' and que[-1]=='S':
        _ = que.pop()
print(len(que))

Submission status: Accepted

Solution code:
from math import ceil,floor,comb,factorial,gcd,pow,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from copy import deepcopy
from time import time
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

x = input()
s = 0
ans = 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:
string = input()
stack = []
for c in string:
    if len(stack) == 0:
        stack.append(c)
        continue
    if stack[-1] + c == 'ST':
        stack.pop()
    else:
        stack.append(c)

print(len(stack))

Submission status: Accepted

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

print(len(X) - 2*ans)

Submission status: Accepted

Solution code:
import sys
import itertools

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_nl = lambda: list(map(int, readline().split()))
in_na = lambda: map(int, read().split())
in_s = lambda: readline().rstrip().decode('utf-8')


def main():
    S = list(in_s())[::-1]
    N = len(S)

    gr = itertools.groupby(S)

    count = 0
    tnum = 0
    for k, v in gr:
        num = sum(1 for _ in v)
        if k == 'S':
            count += min(tnum, num)
            tnum -= min(tnum, num)
        else:
            tnum += num

    # print(count)

    ans = N - count * 2
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
  S = str(input())
  ans = 0
  num_t = 0
  for i in range(len(S)-1, -1, -1):
    if S[i] == 'T':
      flag = True
      num_t += 1
    if S[i] == 'S' and num_t > 0 :
      num_t -= 1
      ans += 2
  print(len(S)-ans)
  
if __name__ == "__main__":
  main()

Submission status: Accepted

Solution code:
x = input()

s, t = 0, 0
for i in x:
    if i == 'S':
        s += 1
    elif i == 'T':
        if s > 0:
            s -= 1
        else:
            t += 1
print(s + t)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x = list(input())
cnt = 0
a = 0
for i in x:
  if i == 'S':
    cnt += 1
  elif i == 'T':
    if cnt > 0:
      cnt -= 1
    else:
      a += 1
print(a+cnt)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
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 
#mod = 998244353
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

X = input()

cnt = 0
s = 0

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

print(len(X)-2*cnt)

Submission status: Accepted

Solution code:
# TLE 1/13 corner0
x = input()
while 'ST' in x:
    x = x.replace('ST', '')
    # 以下の4行を加えて AC  'SSS..STTTTT..T' みたいなやつを検出
    n = len(x) // 2
    if ('T' not in x[:n]) and ('S' not in x[n:]):
        x = ''
        break
print(len(x))

Submission status: Accepted

Solution code:
x = input()
while 'ST' in x:
    x = x.replace('ST', '')
    n = len(x) // 2
    if ('T' not in x[:n]) and ('S' not in x[n:]):
        x = ''
        break
print(len(x))

Submission status: Accepted

Solution code:
x=input()
s,t=0,0

for m in x:
  if m=="S":
    s+=1
  else:
    if s>0:
      s-=1
    else:
      t+=1
print(s+t)

Submission status: Accepted

Solution code:
x = input()

ans = 0
s_cnt = 0

for i in range(len(x)):
    if s_cnt == 0:
        if x[i] == 'T':
            ans += 1
        else:
            s_cnt += 1
    else:
        if x[i] == 'T':
            s_cnt -= 1
        else:
            s_cnt += 1

if s_cnt > 0:
    ans += s_cnt

print(ans)

Submission status: Accepted

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


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

Submission status: Accepted

Solution code:
x = input()

s, t = 0, 0
ols = 0
cnt = 0
for i in x:
  if i == "S":
    s += 1
  else:
    if s > 0:
     cnt += 1
     s -= 1
     s = max(s,0)
print (len(x)-cnt*2)

Submission status: Accepted

Solution code:
s = input()

now_try = 0
idx = 0
while idx < len(s) - 1:

    if s[idx] == 'T':
        idx += 1
        continue

    if s[idx] == 'S':
        if s[idx + 1] == 'T':
            s = s[:idx] + s[idx + 2:]
            now_try += 1
            idx = idx - 1 if idx - 1 >= 0 else 0
        else:
            idx += 1

print(len(s))

Submission status: Accepted

Solution code:
X = input()

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

Submission status: Accepted

Solution code:
def runlength(S):
    A = []
    tmp = S[0]
    cnt = 0
    for i in range(len(S)):
        if S[i] == tmp:
            cnt += 1
        else:
            A.append(cnt)
            tmp = S[i]
            cnt = 1
    else:
        A.append(cnt)
    return A

def main():
    X = input()
    Y = runlength(X)
    if X[0] == 'T':
        Y = [0] + Y
    
    if X[-1] == 'S':
        Y += [0]

    ans = len(X)
    amari = 0
    for i in range(0,len(Y),2):
        ans -= 2 * min(amari + Y[i], Y[i+1])
        amari = max(0, amari + Y[i] - Y[i+1])

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
X = str(input())

Tn = 0
Sn = 0
Nagasa = len(X)

for i in range (0, Nagasa):
	if X[i] == 'T':
		if Sn == 0:
			Tn+=1
		else:
			Sn-=1
	else:
		Sn+=1

print(2*Tn)

Submission status: Accepted

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

t=0
cnt=0
for i in range(n-1,-1,-1):
    if x[i]=='T':
        t+=1
    elif t>0 and x[i]=='S':
        cnt+=1
        t-=1

print(n-2*cnt)

Submission status: Accepted

Solution code:
x = input()
cnt = 0
for x_ in x:
    if x_ == 'T' and cnt:
        cnt -= 1
    elif x_ == 'S':
        cnt += 1
print(2 * cnt)

Submission status: Accepted

Solution code:
X = input()

cntS = 0
res = 0
for x in X:
    if x == "S":
        cntS += 1
    elif cntS > 0:
        cntS -= 1
    else:
        res += 1

print(res + cntS)

Submission status: Accepted

Solution code:
X = input()
 
val = len(X)
sum = 0
for i in X:
    if i == "S":
        sum += 1
    else:
        if sum > 0:
            val -= 2
            sum -= 1
 
print(val)

Submission status: Accepted

Solution code:
X = input()
lis = []
if X[0] == 'S':
    lis.append(0)
cnt = 1
for i in range(1,len(X)):
    if X[i] != X[i-1]:
        lis.append(cnt)
        cnt = 0
    cnt += 1
lis.append(cnt)
if X[-1] == 'T':
    lis.append(0)
else:
    lis += [0,0]
#print(lis)
ans = lis[0]
for i in range(1,len(lis)-2,2):
    if lis[i] < lis[i+1]:
        ans += lis[i+1]-lis[i]
        lis[i+1] -= lis[i]
    else:
        lis[i+2] += lis[i] - lis[i+1]
        lis[i+1] = 0
ans += (lis[-1] + lis[-2])
#print(lis)
print(ans)

Submission status: Accepted

Solution code:
# 05

s = input()
x = 0
y = 0
for i in s:
    if i == 'S':
        y -= 1
    else:
        y += 1
    #print(x, y)
    x = max(x, y)
    
print(x*2)

Submission status: Accepted

Solution code:
def resolve():
    from collections import deque
    x = input()
    final = deque([])
    final.append(x[0])
    for i in range(1, len(x)):
        if x[i] == "S":
            final.append("S")
        else:
            if not final:
                final.append("T")
            else:
                last = final.pop()
                if last == "S":
                    continue
                else:
                    final.append("T")
                    final.append("T")
    print(len(final))
resolve()

Submission status: Accepted

Solution code:
string = input()

length = len(string)
sindex = 0
for i in range(0, length):
  if string[i] == "S": 
    sindex = i
    break
    
s_count = 0

for i in range(sindex, len(string)):
  if string[i] == "S": s_count += 1
  if string[i] == "T" and s_count > 0:
    s_count -= 1
    length -= 2
    
print(length)

Submission status: Accepted

Solution code:
x = input()
#tのラストとsの最初を知りたい
n = len(x)
a = []
for i in range(n):
  if x[i]=="S":
    a.append(x[i])
  else:
    if len(a)!=0 and a[-1]=='S' :
      a.pop()
    else:
      a.append(x[i])
print(len(a))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

def main():
    X = input()
    ans = 0
    now = 0
    for x in X:
        if x == 'S':
            now -= 1
        else:
            now += 1
        ans = max(ans,now)
    print(ans*2)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    X = input()
    cs, ct = 0, 0
    for x in X:
        if x == 'T':
            if cs == 0:
                ct += 1
            else:
                cs -= 1
        else:
            cs += 1
    print(ct * 2)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
X = input()
cnt = 0
for x in X:
    if x == 'T' and cnt:
        cnt -= 1
    elif x == 'S':
        cnt += 1
print(2*cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
X = input()
sc = 0
cnt = 0
ln = len(X)
for i in range(ln):
    if X[i] == 'S':
        sc += 1
    if X[i] == 'T':
        if sc >= 1:
            sc -= 1
            cnt += 1
print(ln - cnt * 2)

Submission status: Accepted

Solution code:
X = list(str(input()))
n = len(X)

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

Submission status: Accepted

Solution code:
X = input()
N = len(X)
c = 0
s, t = 0, 0
for k in range(N):
    if X[k] == "S":
        s += 1
    else:
        t += 1
        if s > 0 and t > 0:
            s -= 1
            t -= 1
            c += 2
print(N-c)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve():
    ans = 0
    X = input()
    num = 0
    for x in X:
        if x=='S':
            num += 1
        else:
            if num>0:
                num -= 1
    ans = num*2
    return ans
print(solve())

Submission status: Accepted

Solution code:
x = input()

cnt = 0
numt = 0
x = x[::-1]
for i in range(len(x)):
    if x[i] == 'T':
        numt += 1
    else:
        if numt > 0:
            cnt += 1
            numt -= 1
print(len(x)-2*cnt)

Submission status: Accepted

Solution code:
S=input()
score=0
maxscore=0
for i in range(len(S)):
  if S[i]=='T':
    score+=1
    maxscore=max(maxscore,score)
  else:
    score-=1
print(2*maxscore)

Submission status: Accepted

Solution code:
X = input()

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
S = input()
k = 0
l = len(S)
for i in S:
  if i == 'S':
    k += 1
  else:
    if k > 0:
      k -= 1
      l -= 2
print(l)

Submission status: Accepted

Solution code:
X = input()

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

print(len(X)-2*cnt)

Submission status: Accepted

Solution code:
# coding: utf-8
X=input()

cnt=0
numS=0
for i in range(len(X)):
    if X[i]=="S":
        numS+=1
    elif numS>0:
        cnt+=1
        numS-=1

print(len(X)-2*cnt)

Submission status: Accepted

Solution code:
s = input()

S = 0
T = 0

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
S = input()

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

Submission status: Accepted

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

def main():
    S = input()
    cnt = 0
    ret = len(S)
    for a in S:
        if a == 'S':
            cnt += 1
        else:
            if cnt>0:
                cnt-=1
                ret-=2
    print(ret)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

print(s+t)

Submission status: Accepted

Solution code:
S = input()

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

print(ans)

Submission status: Accepted

Solution code:
X = list(input())

cnt = 0
s = 0
for i in range(len(X)):
    if X[i] == "S":
        s += 1
    else:
        if s:
            cnt += 2
            s -= 1
print(len(X)-cnt)

Submission status: Accepted

Solution code:
import sys
X = sys.stdin.readline().rstrip()
S = 0
T = 0
ans = 0
for x in X:
    if x == "S":
        S += 1
    else:
        if not S:
            ans += 1
        else:
            S -= 1
ans += S
print(ans)

Submission status: Accepted

Solution code:
def main():
    X = input()
    delcnt = 0
    delans = 0
    for i in range(len(X)):
        if X[i]=='S':
            delcnt += 1
        if X[i]=='T' and delcnt>0:
            delans +=2
            delcnt -=1
    print(len(X)-delans)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
x = input()
res = [x[0]]
for i in x[1:]:
  if i == 'S':
    res += [i]
  elif not res:
    res += [i]
  else:
    if res[-1] == 'S':
      res.pop()
    else:
      res += [i]
print(len(res))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from collections import deque


def solve(X: str):
    stack = deque(X[0])
    for i in range(1, len(X)):
        if not stack:
            stack.append(X[i])
            continue
        if stack[-1] == "T":
            stack.append(X[i])
            continue
        if X[i] == "T":
            stack.pop()
            continue
        stack.append(X[i])

    print(len(stack))


# Generated by 1.1.7.1 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:
X=input()
ans=len(X)
count_s=0
for i in X:
    if i=='S':
        count_s+=1
    if i=='T' and count_s>=1:
        count_s-=1
        ans-=2

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main(X):
    cntS = 0; cntT = 0
    for x in X:
        if x == 'T' and cntS == 0:
            cntT += 1
        elif x == 'S':
            cntS += 1
        elif x == 'T' and cntS > 0:
            cntS -= 1
        else:
            pass
    return cntS + cntT
    
if __name__ == "__main__":
    X = list(input())
    ans = main(X)
    print(ans)

Submission status: Accepted

Solution code:
seq=input()

s=0
t=0
c=0

for si in seq:
    if si=="S":
        s+=1
    else:
        if s==0:
            c+=1
        else:
            s-=1
c+=s

print(c)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####MakePrimes######
def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum

#####GCD#####
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############

S = S()
SUM = 0
MAX = 0
for s in S:
  if s == "T":
    SUM += 1
    MAX = max(MAX,SUM)
  else:
    SUM -= 1
print(MAX*2)

Submission status: Accepted

Solution code:
x = str(input())
cnt = 0
tmp = 0
for i in x:
    if i == 'T':
        if tmp > 0:
            tmp -= 1
        else:
            cnt += 1
    else:
        tmp += 1
else:
    print(cnt + tmp)

Submission status: Accepted

Solution code:
x = list(map(str, input().rstrip()))

stock = 0
rest = 0
for i in x:
    if i == "S":
        stock += 1
    elif i == "T" and stock >= 1:
        stock -= 1
    else:
        rest += 1

print(stock + rest)

Submission status: Accepted

Solution code:
x = input()
ls = []
ans = len(x)
for i in range(len(x)):
    if x[i] == "S":
        ls.append("S")
    else:
        if len(ls):
            ls.pop()
            ans -= 2
print(ans)

Submission status: Accepted

Solution code:
x = input()

pair_cnt = 0
s_cnt = 0
for i in x:
    if i == "S":
        s_cnt += 1
    elif i == "T" and s_cnt:
        pair_cnt += 1
        s_cnt -= 1

print(len(x) - pair_cnt * 2)

Submission status: Accepted

Solution code:
X = list(input())

st = []
for x in X:
    if x == 'S':
        st.append(x)
    else:
        if len(st) > 0 and st[-1] == 'S':
            st.pop()
        else:
            st.append(x)
print(len(st))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

# import re
import math
import collections
# import decimal
import bisect
import itertools
import fractions
# import functools
import copy
import heapq
import decimal
# import statistics
import queue
import numpy as np

sys.setrecursionlimit(10000001)
INF = 10 ** 16
MOD = 10 ** 9 + 7

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


# ===CODE===


def main():
    x = list(input())
    s_cnt = 0

    ans = len(x)
    for xi in x:
        if xi == "S":
            s_cnt += 1
        else:
            if s_cnt != 0:
                s_cnt -= 1
                ans -= 2

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input()
cur = 0
deletedpairs = 0
for i in range(len(s)):
    #print(i, deletedpairs)
    if s[i] == "S":
        cur += 1
    if s[i] == "T":
        if cur > 0:
            deletedpairs += 1
            cur -= 1
        else:
            pass

#print(deletedpairs)
print(len(s) - deletedpairs*2)

Submission status: Accepted

Solution code:
S = input()

num_s = 0

ans = 0
for c in S:
    if c == "S":
        num_s += 1
    else:
        if num_s > 0:
            num_s -= 1
        else:
            ans += 1

ans += num_s

print(ans)

Submission status: Accepted

Solution code:
x = input()
s_cnt = 0
t_cnt = 0
ans = 0
tmp = 0
for s in x:
    if s == "T":
        if tmp == 0:
            ans += 2
        else:
            tmp -= 1
    else:
        tmp += 1
print(ans)