Problem:
 Score : 300 points    Problem Statement We have a string X , which has an even number of characters. Half the characters are S , and the other half are T .  Takahashi, who hates the string ST , will perform the following operation 10^{10000} times:   Among the occurrences of ST in X as (contiguous) substrings, remove the leftmost one. If there is no occurrence, do nothing.   Find the eventual length of X .      Constraints  2 ≦ |X| ≦ 200,000  The length of X is even.  Half the characters in X are S , and the other half are T .       Partial Scores  In test cases worth 200 points, |X| ≦ 200 .         Input The input is given from Standard Input in the following format:  X       Output Print the eventual length of X .        Sample Input 1 TSTTSS      Sample Output 1 4  In the 1 -st operation, the 2 -nd and 3 -rd characters of TSTTSS are removed. X becomes TTSS , and since it does not contain ST anymore, nothing is done in the remaining 10^{10000}-1 operations.
Thus, the answer is 4 .       Sample Input 2 SSTTST      Sample Output 2 0  X will eventually become an empty string: SSTTST ⇒ STST ⇒ ST ⇒ ``.       Sample Input 3 TSSTTTSS      Sample Output 3 4  X will become: TSSTTTSS ⇒ TSTTSS ⇒ TTSS .

Solution (Python):


import sys, re
from collections import deque, defaultdict, Counter
from math import sqrt, hypot, factorial, pi, sin, cos, radians
if sys.version_info.minor >= 5: from math import gcd
else: from fractions import gcd 
from heapq import heappop, heappush, heapify, heappushpop
from bisect import bisect_left, bisect_right
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from functools import reduce, partial
from fractions import Fraction
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def ceil(a, b=1): return int(-(-a // b))
def round(x): return int((x*2+1) // 2)
def fermat(x, y, MOD): return x * pow(y, MOD-2, MOD) % MOD
def lcm(x, y): return (x * y) // gcd(x, y)
def lcm_list(nums): return reduce(lcm, nums, 1)
def gcd_list(nums): return reduce(gcd, nums, nums[0])
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

X = input()
N = len(X)

cnt = 0
s = 0
for i in range(N):
    if X[i] == 'S':
        s += 1
        cnt += 1
    elif X[i] == 'T':
        if s != 0:
            s -= 1
            cnt -= 1
        else:
            cnt += 1
print(cnt)

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

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

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

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

Solution (Python):
a = input()
s = 0
r=0
for i in a:
    if i == "S":
        s+=1
    if i == "T" and s > 0:
        s-=1
        r+=1
print(len(a)-2*r)

Solution (Python):


text = list(input())
txt_stuck = []

for i in text:
    if i == 'S':
        txt_stuck.append(i)
    elif i == 'T' and txt_stuck == []:
        txt_stuck.append(i)
    elif i == 'T' and txt_stuck[-1] == 'T':
        txt_stuck.append(i)
    elif i == 'T' and txt_stuck[-1] == 'S':
        txt_stuck.pop()

print(len(txt_stuck))

Solution (Python):
S_count = 0

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

print(2 * S_count)

Solution (Python):
X = input()

S_count = 0
T_count = 0
for item in X:
    if item == 'S':
        S_count += 1
    elif item == 'T' and S_count > 0:
        S_count -= 1


print(2 * S_count)

Solution (Python):





x = list(input())
n = 0

for i in range(len(x)):
  if x[i] == 'S':
    n += 1
  elif n>0:
    n -= 1

print(n*2)

Solution (Python):
x = list(input())
c = 0
for i in x:
  if i == "S":
    c += 1
  elif c>0:
    c -= 1
print(2*c)

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

Solution (Python):
S = input()

tmp = []
ans = 0

for i in range(len(S)):
    if S[i] == 'S':
        tmp.append(S[i])
        ans = ans + 1
    else:
        if ans == 0:
            tmp.append(S[i])
            ans = ans + 1
        elif tmp[ans-1] == 'T':
            tmp.append(S[i])
            ans = ans + 1
        else:
            tmp.pop(ans-1)
            ans = ans-1
            
print(ans)

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

Solution (Python):
X=input()

cntS=0
SUM=0
for i in range(len(X)):
    if X[i]=="T":
        if cntS>0:
            cntS-=1
        else:
            SUM+=1
    if X[i]=="S":
        cntS+=1
print(SUM*2)

Solution (Python):
V = str(input())
k = []
for i in V:
    if i == "S":
        k.append("S")
    elif k.__len__() == 0 or k[-1] == "T":
        k.append("T")
    else:
        k.pop()
print(len(k))

Solution (Python):

n_S, n_T = 0, 0

for c in input():
    if c == "S":
        n_S += 1
    else:
        if n_S:
            n_S -= 1
        else:
            n_T += 1
print(n_S + n_T)

Solution (Python):

X = input()
tmp = " "
cnt = 0
for x in X:
    if x == "S":
        tmp += x
        cnt += 1
    elif x == "T" and tmp[-1]=="S":
            cnt -= 1
            tmp = tmp[0:len(tmp)-1]
    else:
        tmp += x
        cnt += 1
print(cnt)

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

Solution (Python):
_x = input()
x = []
for i in _x:
    x.append(i)
count = 0



stack = []
i = 0
while i < len(x):
    if len(stack) > 0 and stack[-1] == "S" and x[i] == "T":
        del stack[-1]
    else:
        stack.append(x[i])
    i += 1
print(len(stack))

Solution (Python):
X = input()
ans = ''
for i in range(len(X)):
    ans += X[i]
    if ans[-2:] == 'ST':
        ans = ans[:-2]
print(len(ans))

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

Solution (Python):
s = input()
a = []
for i in s:
  a.append(i)
  if len(a) >= 2 and a[-1] == "T" and a[-2] == "S":
    a.pop()
    a.pop()
print(len(a))

Solution (Python):
s=input()

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

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

a = 0
c = 0
for i in range(n):
    if x[n-1-i] == 'S':
        if a >= 1:
            a -=1
            c += 1
    else:
        a += 1

print(n-c*2)

Solution (Python):
X = input()
ctr = 0
ans = 0
for i in X:
    if i == 'T':
        if ctr > 0:
            ctr -= 1
        else: 
            ans += 1
    else:
        ctr += 1
print(2*ans)

Solution (Python):
S=input()
t=0
u=0
for v in S:
    if v=="S":
        t+=1
    else:
        if t>=1:
            u+=2
            t-=1
print(len(S)-u)

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

Solution (Python):

x = input()
n = len(x)
r = 0
cnt = 0
for i in range(n):
    if x[i]=="S":
        cnt += 1
    else:
        if cnt>0:
            r += 2
            cnt -= 1

print(n-r)

Solution (Python):
X=input().strip()
N=len(X)
sl=0
tl=0
sp=0
for i in range(N-1,-1,-1):
    if X[i]=='S':
        if tl==0:
            sl+=1
        else:
            tl-=1
    else:
        tl+=1
print(sl+tl)

Solution (Python):
s = input()
N = len(s)
cnt = 0
pair = 0
for c in s:
  if c == 'T':
    if cnt > 0:
      cnt -= 1
      pair += 1
  else:
    cnt += 1
print(N-2*pair)

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

Solution (Python):
X = input()
st = 0
s = 0
t = 0
for x in X:
    if x == "S":
        st += min(s, t)
        if t > 0:
            s -= min(s, t)
            t = 0
        s += 1
    elif x == "T" and s > 0:
        t += 1
st += min(s, t)
print(len(X) - 2 * st)

Solution (Python):
X = input()
TS = [0 for i in range(len(X) + 1)]
for i in range(len(X)):
    TS[i+1] = TS[i] + (1 if X[i] == "T" else -1)
print(2*max(TS))

Solution (Python):
X=input()
L = len(X)
ans = L
stack = 0
for i in range(L):
  if X[i] == "T" and stack:
    ans -= 2
    stack -= 1    
  if X[i] == "S":
    stack += 1
print(ans)

Solution (Python):
x=input();c=0
for t in x:c+=t=='S'or(c>0)*-1
print(2*c)

Solution (Python):
x=input();c=0
for t in x:c+=t=='S'or(c>0)*-1
print(2*c)

Solution (Python):
X = input()

x_length = len(X)
stack = []

for i in range(x_length):
    x = X[i]
    if x == "S":
        stack.append(x)

    elif x == "T" and stack and stack[-1] == "S":
        stack.pop()

    else:
        stack.append(x)
print(len(stack))

Solution (Python):
import sys


def main() -> None:
    x = input()
    s_cnt = 0
    ans = 0

    for v in x:
        if v == 'S':
            s_cnt += 1
        if v == 'T':
            if s_cnt == 0:
                ans += 1
            else:
                s_cnt -= 1

    ans += s_cnt
    print(ans)


def next_str() -> str:
    result = ""
    while True:
        tmp = sys.stdin.read(1)
        if tmp.strip() != "":
            result += tmp
        elif tmp != '\r':
            break
    return result


if __name__ == '__main__':
    main()

Solution (Python):
import sys


def main() -> None:
    x = next_str()
    s_cnt = 0
    ans = 0

    for v in x:
        if v == 'S':
            s_cnt += 1
        if v == 'T':
            if s_cnt == 0:
                ans += 1
            else:
                s_cnt -= 1

    ans += s_cnt
    print(ans)


def next_str() -> str:
    result = ""
    while True:
        tmp = sys.stdin.read(1)
        if tmp.strip() != "":
            result += tmp
        elif tmp != '\r':
            break
    return result


if __name__ == '__main__':
    main()

Solution (Python):
s=input()
ans=0
count=0
for i in s:
    if i=='S':count=max(1,count+1)
    else:count-=1
    if count<0:ans+=1
print(ans*2)

Solution (Python):
X = list(input())

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

	if c >= ans:
		ans = c

print(ans * 2)

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

Solution (Python):
Xs = input()

ans = len(Xs)
numS = 0
for X in Xs:
    if X == 'S':
        numS += 1
    else:
        if numS > 0:
            ans -= 2
            numS -= 1

print(ans)

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

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

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

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

Solution (Python):
S = input()

S_c, T_c = 0, 0


ans = 0
for ch in S:
  if ch == "T" and S_c > 0:
    S_c -= 1
  elif ch == "T" and S_c == 0:
    ans += 2
  else:
    S_c += 1
    
print(ans)

Solution (Python):
s = list(input())
l = []
for i in range(len(s)):
    if s[i] == 'S':
        l.append('S')
    elif not l:
        l.append('T')
    elif l[-1] == 'T':
        l.append('T')
    else:
        l.pop()
print(len(l))

Solution (Python):
x = list(input())
c = 0
for i in x:
  if i == "S":
    c += 1
  elif c>0:
    c -= 1
print(2*c)

Solution (Python):
S = input()
ans = len(S)

stack = []
for s in S:

    if s == 'S':
        stack.append(s)
    elif s == 'T' and len(stack) == 0 or stack[-1] == 'T':
        stack.append(s)
    elif s == 'T' and stack[-1] == 'S':
        stack.pop()

print(len(stack))

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

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

Solution (Python):
X = input()
tcnt = 0
scnt = 0
ans = 0
for i in range(0, len(X)):
    
    if X[i] == "S":
        scnt += 1
    elif scnt > tcnt and X[i] == "T":
        tcnt += 1
    else:
        tcnt = 0
        scnt = 0
    if scnt >= tcnt:
        ans += tcnt * 2
        scnt -= tcnt
        tcnt = 0

print(len(X) - ans)

Solution (Python):
s = input()
bra = 0
num = 0
for i in range(len(s)):
    if s[i] == "S":
        bra += 1
    else:
        if bra > 0:
            bra -= 1
            num += 2

print(len(s) - num)

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

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

Solution (Python):
S = []
for c in input():
    if c == 'S':
        S.append(1)
    else:
        if S and S[-1] == 1:
            S.pop()
        else:
            S.append(0)
print(len(S))

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

Solution (Python):
s = input()
stack = [s[0]]
for i in range(1,len(s)):
    if len(stack)==0:
        stack.append(s[i])
        continue
    else:
        cur_str = stack[-1]
        new_str = s[i]
        if cur_str+new_str=="ST":
            stack.pop()
        else:
            stack.append(new_str)
print(len(stack))

Solution (Python):

import sys
stdin = sys.stdin

def li(): return [int(x) for x in stdin.readline().split()]
def li_(): return [int(x)-1 for x in stdin.readline().split()]
def lf(): return [float(x) for x in stdin.readline().split()]
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(ns())
def nf(): return float(ns())

x = lc()


s_cnt = 0
ans = 0
for c in x:
    if c == "S":
        s_cnt += 1
    else:
        s_cnt -= 1
        
        if s_cnt < 0:
            ans += 1
            s_cnt = 0

ans += s_cnt

print(ans)

Solution (Python):
stack = []

for c in input():
    if c=='S' or len(stack)==0:
        stack.append(c)
    elif stack[-1]=='S':
        stack.pop()
    else:
        stack.append(c)
    

print(len(stack))

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

for v in X:
    if v == 'S':
        a.append(v)
    if v == 'T':
        if len(a) == 0 or a[-1] == 'T':
            a.append(v)
        elif a[-1] == 'S':
            assert a.pop()

print(len(a))

Solution (Python):
X = input()
cnt = 0
s_cnt = 0
t_cnt = 0

for v in X:
    if s_cnt == 0 and v == 'T':
        cnt += 1
        t_cnt = 0
        continue

    if v == 'S':
        s_cnt += 1
    if v == 'T':
        t_cnt += 1

    if t_cnt == s_cnt:
        s_cnt = 0
        t_cnt = 0

print(cnt + s_cnt - t_cnt)

Solution (Python):
from collections import deque
q=deque([])
c=0
for i in input():
  if i=="S": 
    q.append(1)
  else: 
    if q:
      q.pop()
    else:
      c+=1

print(c+len(q))

Solution (Python):
x = input()

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

Solution (Python):
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpl_s(): return list(input().split())

S = input()
ans = 0
tmp = 0
for s in S:
	if s == 'S':
		tmp += 1
	elif s == 'T':
		tmp -= 1
		if tmp < 0:
			tmp = 0
			ans += 1

print(ans+tmp)

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

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

Solution (Python):
X = input()

result = 0

scount = 0

for c in X:
    if c == "T":
        if scount >= 1:
            scount -= 1
        else:
            result += 1
    else:
        scount += 1

print(result * 2)

Solution (Python):
def getinputdata():

    array_result = []    
    data = input()
    array_result.append(data.split(" "))

    flg = True

    try:
        while flg:

            data = input()

            if(data != ""):
                
                array_result.append(data.split(" "))

                flg = True

            else:

                flg = False
    finally:

        return array_result

arr_data = getinputdata()

x=arr_data[0][0]

arr=[]


for v in x:
    

    if v=="S":
        
        arr.append("S")
        

    elif v=="T" and (len(arr)==0 or arr[-1]=="T"):
        
        arr.append("T")
      
    else:
            
        del arr[-1]
        
            
print(len(arr))

Solution (Python):
from collections import deque
def getinputdata():

    array_result = []    
    data = input()
    array_result.append(data.split(" "))

    flg = True

    try:
        while flg:

            data = input()

            if(data != ""):
                
                array_result.append(data.split(" "))

                flg = True

            else:

                flg = False
    finally:

        return array_result

arr_data = getinputdata()

x=arr_data[0][0]

b = deque([])

for v in x:
    
    if v=="S":
        
        b.appendleft(v)
        
    elif v=="T" and (len(b)==0 or b[0]=="T"):
        
        b.appendleft(v)
        
    else:
            
        b.popleft()
            
print(len(b))

Solution (Python):
x = input()

forward = 0
back = 0
res = 0
for char in x:
    if char == "S":
        forward += 1
    else:
        if forward == 0:
            res += 1
        else:
            forward -= 1

print(res+forward)

Solution (Python):
from collections import deque
S=input()
table=deque()
table.append(1)
ans=0
for s in S:
    if s=='S':
        table.append(1)
        
    else:
        if len(table)==1:
            table[0]==1
           
        else:
            table.pop()
            ans+=table[-1]
            
            
print(len(S)-2*ans)

Solution (Python):
s = input()
a = []
ans = len(s)
for i in s:
	if i=='S':
		a.append(1)
	elif len(a)!=0:
		a.pop()
		ans -= 2
print(ans)

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

Solution (Python):
s = raw_input()

stk = []

ans = 0

for string in s:
	if string == 'S':
		stk.append('S')

	else :
		if len(stk) != 0:
			stk.pop()

		else :
			ans += 1

print ans + len(stk)

Solution (Python):
x = list(input())
ans = 0
gauge = 0

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

print(ans + gauge)

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

    
    ans = editorial(X)
    print(ans)


def test_ans():
    X_ans = [
        ("TSTTSS", 4),
        ("SSTTST", 0),
        ("TSSTTTSS", 4),
    ]
    for X, ans in X_ans:
        assert part(X) == editorial(X) == ans


def editorial(X):
    stack = [X[0]]
    ans = len(X)
    for c in X[1:]:
        if stack and stack[-1] == "S" and c == "T":
            ans -= 2
            stack.pop()
        else:
            stack.append(c)

    return ans


def part(X):
    while True:
        n = len(X)
        for i in range(n-1):
            if X[i:i+2] == "ST":
                X = X[:i] + X[i+2:]
                break

        if n == len(X):
            break

    return len(X)


if __name__ == '__main__':
    main()

Solution (Python):
x = input()
s = 0
remain = 0
for i in x:
  if i == 'S':
    s += 1
  elif i == 'T' and s >= 1:
    s -= 1
  else:
    remain += 1

print(s + remain)

Solution (Python):
from collections import deque


def main():
    x = input()

    st, cnt = deque(), 0
    for i in range(len(x)):
        if x[i] == "S":
            st.append(i)
        elif len(st) != 0:
            st.pop()
            cnt += 1

    print(len(x)-2*cnt)


if __name__ == '__main__':
    main()

Solution (Python):






if __name__ == '__main__':
    from collections import deque

    x = input()
    n = len(x)
    d = deque()

    for i in range(n):
        d.append(x[i])

        if len(d) >= 2 and d[-2] + d[-1] == 'ST':
            d.pop()
            d.pop()

    print(len(d))

Solution (Python):
X = input().strip()

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

Solution (Python):
x=list(input())
stack=[]
t_f=[]
for i in range(len(x)):
    if x[i]=="S":
        stack.append(x[i])
    elif x[i]=="T":
        if len(stack)!=0:
            stack.pop()
        else:
            t_f.append(x[i])
print(len(stack)+len(t_f))

Solution (Python):
X = input()

A = ""
for x in X:
    if x == "T" and A != "" and A[-1] == "S":
        A = A[:-1]
    else:
        A = A + x

print(len(A))

Solution (Python):
n = input()

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

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

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

Solution (Python):
X=input()

ans=0
s=0
for i in range(len(X)):
  if X[i]=='T' and s==0:
    ans+=1
  elif X[i]=='T' and s>0:
    s-=1
  else:
    s+=1

print(ans+s)

Solution (Python):
X = input()

T = 0
S = 0
TS = True
for x in X:
    if x=="T":
        if TS:
            T += 1
        else:
            S -= 1
            if S==0:
                TS = True
    else:
        if TS: TS = False
        S += 1
    
print(T+S)

Solution (Python):
a=input()
x=0
b=0
for i in a:
  if i=="T" and x<=-1:
    x+=1
  elif i=="S":
    x-=1
  elif x==0:
    b+=1
print(b*2)

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

ans = n
temp_s = 0

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

print(ans)

Solution (Python):
s = input()
n = len(s)
ans = n
flag = 0
temp_s = 0

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

print(ans)

Solution (Python):


import sys, math, copy




HUGE = 2147483647
HUGEL = 9223372036854775807
ABC = "abcdefghijklmnopqrstuvwxyz"

def main():
    x = input()
    lev = 0
    for c in x:
        if c == "S":
            lev += 1
        else:
            lev -= 1
            lev = max(0, lev)
    print(lev * 2)

main()

Solution (Python):




str = input()
a = list(str)

s=0
t=0

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

Solution (Python):
X = input()
countS = 0
countT = 0
for i in X:
    if i == "S":
        countS += 1
    else:
        if countS > 0:
            countS -= 1
        else:
            countT += 1
print(countS + countT)

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

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

Solution (Python):
S = input()
stack = []
for s in S:
    if stack and stack[-1]=="S" and s=="T":
        stack.pop()
    else:
        stack.append(s)

print(len(stack))

Solution (Python):
x = input()
answer = 0
for i in x:
	if i=="S": answer += 1
	elif i=="T" and answer!=0: answer-=1
	else: pass
print(2*answer)

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

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

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

Solution (Python):
X=input()
N=len(X)
k=0
a=0
for i in range(N):
    if X[i]=="S":
       a+=1
    else:
        if a!=0:
            k+=2
            a-=1
print(N-k)

Solution (Python):
def finalLength(sample):
    foo = len(sample)
    tmp = 0
    for c in sample:
        if c=='S':
            tmp += 1
        elif tmp>0:
            tmp -= 1
            foo -= 2
    return foo

def main():
    sample = raw_input().strip()
    print finalLength(sample)

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    s = input()
    
    A = []
    for i in range(len(s)):
        if s[i] == "S":
            A.append("S")
        else:
            if len(A) > 0 and A[-1] == "S":
                A.pop()
            else:
                A.append("T")
    print(len(A))


if __name__ == "__main__":
    main()

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

Solution (Python):

X = input()
sub = 0
tmp = 0
s, t, = 0, 0
for c in X:
    if c == 'S':
        s += 1
    else:
        t += 1
        if s > 0:
            s -= 1
            t -= 1
print(s + t)

Solution (Python):
a = list(input())

n = 0
res = 0
for x in a:
    if x == 'T':
        if n == 0:
            res += 1
        elif n > 0:
            n -= 1
    elif x == 'S':
        n += 1
res += n

print(res)

Solution (Python):
X= input()
T_count=X.count("T")
S_count=X.count("S")
T_removed=0
i=0
ans=0
while i<len(X):
    if X[i]=="T":
        if T_removed>0:
            T_removed -=1
        else:
            T_count -= 1
            ans+=1
    elif X[i]=="S":
        if T_count:
            T_count-=1
            T_removed+=1
        else:
            ans+=1
    i+=1
print(ans)

Solution (Python):
X = list(input())

C = 0
res = 0
for x in X:
	if x == 'T':
		if C == 0:
			res += 1
		elif C > 0:
			C -= 1
	elif x == 'S':
		C += 1
res += C

print(res)

Solution (Python):
X = list(input())

C = 0
res = 0
for x in X:
	if x == 'T':
		if C == 0:
			res += 1
		elif C > 0:
			C -= 1
	elif x == 'S':
		C += 1
res += C

print(res)

Solution (Python):
S = input()

ret = 0

cnt = 0


for s in S:
    if s == 'S':
        cnt += 1
    else:
        if cnt > 0:
            cnt -= 1
        else:
            ret += 1

cnt = 0

for s in S[::-1]:
    if s == 'T':
        cnt += 1
    else:
        if cnt > 0:
            cnt -= 1
        else:
            ret += 1

print(ret)

Solution (Python):
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
INF = float("inf")


def main():
    X = input()

    ans, s = 0, 0
    for i in range(len(X)):
        if X[i] == "S":
            s += 1
        elif X[i] == "T" and s > 0:
            s -= 1
            ans += 2
    print(len(X) - ans)


if __name__ == '__main__':
    main()

Solution (Python):

x = input()
s = 0
t = 0
for c in x:
    if c == 'S':
        s += 1
    elif c == 'T':
        if s:
            s -= 1
        else:
            t += 1
    else:
        assert False
print(s + t)

Solution (Python):
X = input()

count = 0
t_count = 0
for x in X:
    if x == "S":
        count += 1
    if x == "T":
        count -= 1
        if count < 0:
            count = 0
            t_count += 1


print(t_count + count)

Solution (Python):
if __name__ == "__main__":
    X = input()
    S = 0
    T = 0
    res = len(X)
    for x in X:
        if (x == 'S'):
            S += 1
        else:
            if (S > 0):
                S -= 1
                res -= 2
    print (res)

Solution (Python):
def ST_string_2():
	x = str(raw_input())
	stack = ['
	for c in x:
		if c=="S":
			stack.append(c)
		else:
			if stack[-1]=="S":
				stack.pop()
			else:
				stack.append(c)
	print len(stack)-1

if __name__ == "__main__":
	ST_string_2()

Solution (Python):

X = input()











i = 0
cnt = []
X += " "
cur = 1
for i in range(len(X) - 1):
    if X[i] == X[i + 1]:
        cur += 1
    else:
        cnt.append((X[i], cur))
        cur = 1

total = 0
for i in range(len(cnt) - 1):
    if cnt[i][0] == 'S':
        if cnt[i][1] > cnt[i + 1][1]:
            if i + 2 < len(cnt):
                cnt[i + 2] = (cnt[i + 2][0], cnt[i + 2][1] + cnt[i][1] - cnt[i + 1][1])
        total += min(cnt[i][1], cnt[i + 1][1])
print(len(X) - 1 - 2 * total)

Solution (Python):
s = input()
ret = 0
tmp = 0
for c in s:
    tmp += 1 if c == 'S' else -1
    ret = min(ret, tmp)

print(-ret*2)

Solution (Python):

import math
import sys
import itertools
import numpy as np
import functools
import collections
mo = 1000000007
r = range

s = input()
ret = 0
tmp = 0
for c in s:
    tmp += 1 if c == 'S' else -1
    ret = min(ret, tmp)

print(-ret*2)