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()
lS = len(S)
l = []
c = -1
for i in range(lS):
    if (c == -1):
        l.append(S[i])
        c += 1
    elif (S[i] == "S"):
        l.append(S[i])
        c += 1
    elif (S[i] == "T"):
        if (l[c] == "S"):
            l.pop()
            c -= 1
        else:
            l.append(S[i])
            c += 1
            
print(c+1)

Submission status: Accepted

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

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

print(len(L))

Submission status: Accepted

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

ans=0
s_used=0
t_owe=0
for i in range(n):
    if a[i]=="S":
        if t_count[i]-t_owe>0:
            ans+=2
            s_used+=1
            t_owe+=1
    else:
        if t_owe>0:
            t_owe-=1
    #print('ans {} s_used {} t_owe {} '.format(ans,s_used,t_owe))
print(n-ans)

Submission status: Accepted

Solution code:
x=input()

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:

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

Submission status: Accepted

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

    ans = ''

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

    print(len(ans))

Submission status: Accepted

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

Submission status: Accepted

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


if __name__ == "__main__":
    solve()

Submission status: Accepted

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
X = input()

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

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

print(len(X) - cnt)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
S = input()

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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


print(n)

Submission status: Accepted

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


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


def S(): return input()


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


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


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


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


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


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


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


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


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


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

show_flg = False
# show_flg = True


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

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

    print(len(X) - c * 2)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

impossible = 0
num_s = 0

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

print(impossible)

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
    else:
        if s_count > 0:
            ans += 1
            s_count -= 1
print(len(x) - ans*2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

for char in s:
    if len(stack) == 0:
        stack.append(char)
        continue
    
    if stack[-1] + char == "ST":
        stack.pop(-1)
    else:
        stack.append(char)
print(len(stack))

Submission status: Accepted

Solution code:
x=input()

c_s=0
c_t=0

for i in x:
  if i=='S':
    c_s+=1
  else:
    if c_s==0:
      c_t+=1
    else:
      c_s-=1

print(c_s+c_t)

Submission status: Accepted

Solution code:
x = input()

s = [""]
for xi in x:
    if xi == "S":
        s.append(xi)
    elif xi == "T":
        if s[-1] == "S":
            s.pop()
        else:
            s.append(xi)

print(len(s) - 1)

Submission status: Accepted

Solution code:
X=input()
level=0
rem=0
for i,s in enumerate(X):
    if s=="S":
        level+=1
    else:
        if level>0:
            level-=1
            rem+=1
print(len(X)-rem*2)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline
s = readline().rstrip().decode('utf-8')
n = len(s)
s_count = 0
ans = 0
i=0
while True:
    if i == n:
        break
    if s[i] == "T":
        if s_count > 0:
            s_count-=1
            ans+=2
    else:
        s_count+=1
    i+=1


print(n-ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x = input()
x = "T" + x + "S"
ar = []
c = 1
for i in range(1, len(x)):
    if x[i-1] == x[i]:
        c += 1
    else:
        ar.append(c)
        c = 1
ar.append(c)

ssum = ar[1]
trem = ar[0]
for i in range(2, len(ar), 2):
    trem += max(0, ar[i]-ssum)
    ssum -= min(ssum, ar[i])
    ssum += ar[i+1]
print(ssum+trem-2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 前から順に見ていき、Tなら+1点、Sなら-1点とする。
# 最後まで見たときの途中の最高得点が、最後に残るTの個数。なので答えはその2倍。

# 理由：
# 最高得点a点とすると、その左側ではTがSよりもa個多いので、そのa個は消せない。右側にいくらSがあっても関係ない。
# その他が全部消える理由はイメージ的にはそんな気がするが、ちゃんとは分からない……

s = input()
score = max_score = 0
for ch in s:
    if ch == "T":
        score += 1
        max_score = max(score, max_score)
    elif ch == "S":
        score -= 1
print(max_score * 2)

Submission status: Accepted

Solution code:
from collections import deque
def main():
    X = input()
    stack = deque([])
    for i in range(len(X)):
        if X[i] == "S":
            stack.append(X[i])
        else:
            if len(stack) != 0:
                tmp = stack.pop()
                if tmp == "T":
                    stack.append(tmp)
                    stack.append(X[i])
            else:
                stack.append(X[i])
    print(len(stack))
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
x=input()
stack=[]
for k in range(len(x)):
  if x[k]=='S':
    stack.append('S')
  else:
    if (len(stack)>0) and (stack[-1]=='S'):
      stack.pop()
    else:
      stack.append('T')
print(len(stack))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
S = input()

count = len(S)
N = count
s_count = 0
t_count = 0

i = 0
while i < N:
    if S[i] == 'S':
        s_count += 1
        i += 1
    else:
        if s_count != 0:
            tmp = i
            while tmp < N and S[tmp] == 'T':
                tmp += 1
                t_count += 1
            if s_count > t_count:
                cut = t_count
                s_count = s_count - t_count
                t_count = 0
            else:
                cut = s_count
                s_count = t_count = 0
            count -= cut*2
            i += cut
        else:
            i += 1

        
print(count)

Submission status: Accepted

Solution code:
from collections import deque

X = input()
N = len(X)
S = "S"
T = "T"
stack = deque("")

for i,str in enumerate(X):
  if str == S:
    stack.append(str)
  elif str == T:
    if len(stack) == 0:
      stack.append(str)
    elif stack[-1] == T:
      stack.append(str)
    elif stack[-1] == S:
      stack.pop()
      
print(len(stack))

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    X = input().strip()


    scount = 0
    ans = 0
    for s in X:
        if s == "S":
            scount += 1
        elif s == "T":
            if scount == 0:
                continue
            else:
                scount -= 1
                ans += 2

    print(len(X) - ans)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def examA():
    X = SI()
    d = defaultdict(int)
    for s in X:
        if s=="T" and d["S"]>=1:
            d["S"] -=1
        else:
            d[s] += 1
    ans = d["S"] + d["T"]
    print(ans)
    return

def examB():
    ans = 0
    print(ans)
    return

def examC():
    ans = 0
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math,random
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

if __name__ == '__main__':
    examA()

"""

"""

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x = "T"+input()+"S"
ans = len(x)-2
s = t = 0
for i in x:
    if i=="S":
        ans -= min(s,t)*2
        if s>t:
            s -= t
            t = 0
        else: t = s = 0
        s += 1
    else: t += 1
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
X = input()
ans=0
s_cnt=0
for tmp in X:
    if tmp =="T":
        if s_cnt==0:
            ans+=1
        else:
            s_cnt-=1
    if tmp=="S":
        s_cnt+=1
print(ans+s_cnt)

Submission status: Accepted

Solution code:
X = input()

n = 0
ans = len(X)
for c in X:
    if c == 'S':
        n += 1
    elif n > 0:
        n -= 1
        ans -= 2

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
inf = 10**10

s = stdin.readline().rstrip()

point_s = 0
point_t = 0

for i in s:
    if i == "S":
        point_s += 1
    else:
        if point_s:
            point_s -= 1
        else:
            point_t += 1

print(point_s+point_t)

Submission status: Accepted

Solution code:
x=input()
ans=[]
for i in range(len(x)):
    if(x[i]=='S'):
        ans.append(x[i])
    elif(len(ans)>0 and ans[-1]=='S'):
        ans.pop()
    else:
        ans.append(x[i])
print(len(''.join(ans)))

Submission status: Accepted

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

Submission status: Accepted

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

for c in x:
    if c == "S":
        s += 1
    else:
        if s != 0:
            s -= 1
        else:
            ans += 1
else:
    ans += s

print(ans)

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 > 0: s -= 1
        else: ans += 1
print(ans + s)

Submission status: Accepted

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

tcnt_l = 0
scnt_l = 0
for i in range(N):
    if X[i] == "T" and scnt_l == 0:
        tcnt_l += 1
    elif X[i] == "S":
        scnt_l += 1
    else:
        scnt_l -= 1

tcnt_r = 0
scnt_r = 0
for i in range(N-1,-1,-1):
    if X[i] == "S" and tcnt_r == 0:
        scnt_r += 1
    elif X[i] == "T":
        tcnt_r += 1
    else:
        tcnt_r -= 1

print(tcnt_l+scnt_r)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def roundUp(a, b):
        return -(-a // b)

    @staticmethod
    def toUpperMultiple(a, x):
        return Math.roundUp(a, x) * x

    @staticmethod
    def toLowerMultiple(a, x):
        return (a // x) * x

    @staticmethod
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret

    @staticmethod
    def sign(n):
        if n == 0:
            return 0
        if n < 0:
            return -1
        return 1

    @staticmethod
    def isPrime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


class PriorityQueue:
    def __init__(self, l=[]):
        self.__q = l
        heapq.heapify(self.__q)
        return

    def push(self, n):
        heapq.heappush(self.__q, n)
        return

    def pop(self):
        return heapq.heappop(self.__q)


MOD = int(1e09) + 7
INF = int(1e15)


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)

    divisors.sort()
    return divisors


def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a


def main():
    # sys.stdin = open("sample.txt")
    S = Scanner.string()
    cnt = 0
    ans = 0
    for s in S:
        if s == "S":
            cnt += 1
        else:
            if cnt > 0:
                cnt -= 1
                ans += 1
    print(len(S) - ans * 2)
    return


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

def main():
    S = str(input())
    N = len(S)
    sc = 0
    count = 0
    for r in range(N):
        if S[r] == "T":
            if sc == 0:
                continue
            else:
                sc -= 1
                count += 1
        else:
            sc += 1
            
    print(N-2*count)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
 
stdin = sys.stdin

mod = 1000000007
inf = 1 << 60
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()
nas = lambda: stdin.readline().split()

x = ns()
n = len(x)

ans = n
i = 0
cs = 0
while i < n:
    if x[i] == 'S':
        cs += 1
        i += 1
    else:
        ct = 0
        while i < n and x[i] == 'T':
            ct += 1
            i += 1
        ans -= min(cs, ct) * 2
        cs -= min(cs, ct)

print(ans)

Submission status: Accepted

Solution code:
X = input()

result = len(X)
n_S = 0
for c in X:
    if c == 'S':
        n_S += 1
    elif c == 'T':
        if n_S > 0:
           n_S -= 1 
           result -= 2
    else:
        raise ValueError
print(result)

Submission status: Accepted

Solution code:
xl = list(input())

ans = 0
stock = 0
for x in xl:
    if x == "S":
        stock += 1
    else:
        if stock:
            stock -= 1
            ans += 1

print(len(xl)-2*ans)

Submission status: Accepted

Solution code:
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 += 2
        
print(N - ans)

Submission status: Accepted

Solution code:
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 += 2
print(N-ans)

Submission status: Accepted

Solution code:
x = input()

s = list()
cnt = 0
for c in x:
    if c == "S":
        s.append(c)
    elif c == "T" and len(s) != 0:
        s.pop()
        cnt += 1

print(len(x)-cnt*2)

Submission status: Accepted

Solution code:
x = list(input())

cnt_S = 0
cnt_T = 0

for i in range(len(x)):
    if x[i] == "S":
        cnt_S += 1
    
    elif x[i] == "T":
        if cnt_T < cnt_S:
            cnt_T += 1

print(len(x)-2*cnt_T)

Submission status: Accepted

Solution code:
X = input()

stacks = []
for i in range(len(X)):
    if not stacks:
        stacks.append(X[i])
    elif stacks[-1] == "S" and X[i] == "T":
        del stacks[-1]
    else:
        stacks.append(X[i])

print(len(stacks))

Submission status: Accepted

Solution code:
X = input()

ans = 0
cnt = 0
for i in range(len(X)):
    if cnt == 0 and X[i] == "T":
        ans += 1
    else:
        if X[i] == "T":
            cnt += 1
        else:
            cnt -= 1

print(ans*2)

Submission status: Accepted

Solution code:
s=input()

from collections import deque

stack=deque()

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s_list = list(input())
seizon_t = 0
s_amari = 0
for char in s_list:
  if char == "T":
    if s_amari == 0:
      seizon_t += 1
    else:
      s_amari -= 1
  else:
    s_amari += 1
print(2*seizon_t)

Submission status: Accepted

Solution code:
from collections import deque

X = input()

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

Submission status: Accepted

Solution code:



X = input()

N = len(X)

cnt_S = 0
cnt_T = 0
cnt_delete = 0
for i in range(N):
    if i == 0:
        if X[i] == "S":
            cnt_S += 1
        else:
            cnt_T += 1
    else:
        if X[i-1] == "S" and X[i] == "S":
            cnt_S += 1
        elif X[i-1] == "S" and X[i] == "T":
            cnt_T += 1
        elif X[i-1] == "T" and X[i] == "T":
            cnt_T += 1
        elif X[i-1] == "T" and X[i] == "S":
            tmp = min(cnt_S, cnt_T)
            cnt_delete += tmp*2
            cnt_S -= tmp
            cnt_T = 0
            cnt_S += 1

    if i == N-1:
        tmp = min(cnt_S, cnt_T)
        cnt_delete += tmp*2

print(N - cnt_delete)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x = input()

counter = 0
stack = []

for i in range(len(x)):
    if x[i] == "S":
        stack.append("S")
    elif stack:
        stack.pop()
        counter += 1

print(len(x) - 2 * counter)

Submission status: Accepted

Solution code:
X = input()
xlst = []
xlst.append(X[0])
for i in range(1, len(X)):
    if X[i] == 'S':
        xlst.append(X[i])
    else:
        if xlst != [] and xlst[-1] == 'T':
            xlst.append(X[i])
        elif xlst != [] and xlst[-1] == 'S':
            xlst.pop()
        else:
            xlst.append(X[i])

print(len(xlst))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
X=input()
Y=[0]*len(X)
#Sは0,Tは1
for i in range(len(X)):
    if X[i]=='T':
        Y[i]=1
#print(Y)

from collections import deque

Z=deque([])
for i in range(len(X)):
    c=Y[i]
    if c==0:
        Z.append(c)
    elif Z==deque([]):
        Z.append(c)
    elif Z[-1]==1:
        Z.append(c)
    else:
        Z.pop()

print(len(Z))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 最初にでてきたSでフラグ 備蓄がなくなるまでTとペアにしてなくなったらフラグしなおし
ST = input()
flag = False
pool_s = 0
pair = 0
flag = 0
for v in ST:
    if flag == False and v == "S":
        flag = True
        pool_s = 1
    elif flag and v == "T":
        if pool_s > 0:
            pair += 1
            pool_s -= 1
        else:
            pool_s = 0
            flag = False
    elif flag and v == "S":
        pool_s += 1
print(len(ST) - pair * 2)

Submission status: Accepted

Solution code:
#coding:utf-8
X = input()
ans = 0
cnt = 0
for x in X:
    if x=="S":
        cnt+=1
    else:
        if (cnt>0):
            cnt-=1
            ans+=1
print (len(X)-2*ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    x = S()
    ans = len(x)
    l = 0
    for xi in x:
        if xi == "T":
            if l:
                l -= 1
                ans -= 2
        else:
            l += 1
    print(ans)
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
X = input().strip()
cnt = 0
cntmax = 0
for i in range(len(X)):
    if X[i]=="T":
        cnt += 1
        cntmax = max(cntmax,cnt)
    else:
        cnt -= 1
print(cntmax*2)