Problem:
 Score : 300 points    Problem Statement There are N people standing in a row from west to east.
Each person is facing east or west.
The directions of the people is given as a string S of length N .
The i -th person from the west is facing east if S_i =  E , and west if S_i =  W .  You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader.
Here, we do not care which direction the leader is facing.  The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized.
Find the minimum number of people who have to change their directions.      Constraints  2 \leq N \leq 3 \times 10^5  |S| = N  S_i is E or W .         Input Input is given from Standard Input in the following format:  N  S       Output Print the minimum number of people who have to change their directions.        Sample Input 1 5
WEEWW      Sample Output 1 1  Assume that we appoint the third person from the west as the leader.
Then, the first person from the west needs to face east and has to turn around.
The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case.
It is not possible to have 0 people who have to change their directions, so the answer is 1 .       Sample Input 2 12
WEWEWEEEWWWE      Sample Output 2 4       Sample Input 3 8
WWWWWEEE      Sample Output 3 3

Solution (Python):
import os,re,sys,operator
from collections import Counter,deque
from operator import itemgetter
from itertools import accumulate,combinations,groupby
from sys import stdin,setrecursionlimit
from copy import deepcopy

setrecursionlimit(10**6)

n=int(stdin.readline().rstrip())
s=stdin.readline().rstrip()
cntw=[0]*(n+2);cnte=[0]*(n+2)
ans=10**6

for i in range(1,n+1):
    if s[i-1]=="W":
        cntw[i]=cntw[i-1]+1
        cnte[i]=cnte[i-1]
    elif s[i-1]=="E":
        cnte[i]=cnte[i-1]+1
        cntw[i]=cntw[i-1]
sumw,sume=cntw[n],cnte[n]
for i in range(n):
    now=cntw[i]+(sume-cnte[i+1])
    ans=min(ans,now)
print(ans)

Solution (Python):
N = int(input())
S = input()

W = []
for i in range(N):
    if i == 0:
        if "W" == S[i]:
            W.append(1)
        else:
            W.append(0)
    else:
        if "W" == S[i]:
            W.append(1+W[i-1])
        else:
            W.append(0+W[i-1])

E = []
for i in range(N):
    if i == 0:
        if "E" == S[N-1-i]:
            E.append(1)
        else:
            E.append(0)
    else:
        if "E" == S[N-1-i]:
            E.append(1+E[i-1])
        else:
            E.append(0+E[i-1])
E.reverse()

ans = 10 ** 9
for i in range(N):
    ans = min(ans, W[i]+E[i]-1)
print(ans)
\

Solution (Python):
a = int(input())
b = input()
c = [0] * a
d = [0] * a
for i in range(a):
    if i == 0:
        if b[i] == "W":
            c[i] = 1
    else:
        if b[i] == "W":
            c[i] = c[i-1] + 1
        else:
            c[i] = c[i - 1]

    if i == 0:
        if b[-i-1] == "E":
            d[-i-1] = 1
    else:
        if b[-i-1] == "E":
            d[-i-1] = d[-i] + 1
        else:
            d[-i-1] = d[-i]
e = 10 ** 9
for i in range(a):
    e = min(c[i] + d[i] - 1, e)
print(e)

Solution (Python):
n = int(input())
s = input()
num_e = [0]
num_w = [0]
ans = 999999999999

for i in range(n):
    num_w.append(num_w[i])
    if s[i] == "W":
        num_w[i + 1] += 1
    num_e.append(num_e[i])
    if s[i] == "E":
        num_e[i + 1] += 1

for i in range(n):
    now = num_w[i] + num_e[-1] - num_e[i + 1]
    if ans > now:
        ans = now

print(ans)

Solution (Python):
N = int(input())
S = input()
E = S.count("E")
W = N - E

s = N
re = E
lw = 0
  

for i in S:
  if i == "E":
    re -= 1
    s = min(s, re + lw)
  else:
    s = min(s, re + lw)
    lw += 1
print(s)

Solution (Python):
n = int(input())
import sys
sys.setrecursionlimit(1000000+1)
s = input()

ce = {}

def counte(s,i):
    if i in ce:
        return ce[i]
    elif i == n-1:
        return 0
    else:
        ce[i] = counte(s,i+1) + s[i+1:i+2].count("E")
        return ce[i]

cw = {}

def countw(s,i):
    if i in cw:
        return cw[i]
    elif i == 0:
        return 0
    else:
        cw[i] = countw(s,i-1) + s[i-1:i].count("W")
        return cw[i]

minew = n
for i in range(0,n):
    ew = counte(s,i)+countw(s,i)
    if ew < minew:
        minew = ew
print(minew)

Solution (Python):
N = int(input())
S = input()
W,E = 0,S.count("E")
ans,SUM = 0,10**10
for i in range(N):
    s = S[i]
    if s=="W":
        if SUM>W+E:
            SUM = W+E
            ans = i
        W+=1
    else:
        E -= 1
        if SUM>W+E:
            SUM = W+E
            ans = i
print(SUM)

Solution (Python):

N = int(input())
S = list(input())
c = [0] * (N+1)
for i in range(N):
    c[i+1] = c[i]
    if S[i] == 'W':
        c[i+1] += 1

ans = N
for i in range(1,N+1):
    ans = min(ans, c[i-1] + ((N - i) - (c[N] - c[i])))
print(ans)

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


east = [0]*(n+1)
for i in range(n+1):
    if i == 0:
        east[i] = 0
    else:
        if s[i-1] =="E":
            east[i] = east[i-1] +1
        else:
            east[i] = east[i-1]


ans = float("inf")
for i in range(n):
    lefte = east[i]
    righte = east[-1] - east[i+1]
    leftw = i - lefte
    rightw = n - i - 1 - righte
    x = 0
    x += leftw
    x += righte
    if x < ans:
        ans = x
print(ans)

Solution (Python):
N = int(input())
S = input()
ans = [S[1:].count("E")]
for i in range(1, N):
	tmp = ans[i - 1]
	if S[i - 1] == "W":
		tmp += 1
	if S[i] == "E":
		tmp -= 1
	ans.append(tmp)
print(min(ans))

Solution (Python):

def main():
    n = int(input())
    s = input()
    l, r = 0, s[1:].count("E")  
    cnt = l + r
    for i in range(1, n):
        if s[i - 1] == "W":
            l += 1
        if s[i] == "E":
            r -= 1
        cnt = min(cnt, l + r)
    print(cnt)


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
S = input()

rcost = 0
lcost = S.count('E')
if S[0] == 'E':
    lcost -= 1
ans = lcost

for i in range(N-1):
        if S[i] == 'W':
            rcost += 1
        if S[i+1] == 'E':
            lcost -= 1
        ans = min(ans, rcost+lcost)
    
print(ans)

Solution (Python):
N = int(input())
S = input()

rcost = 0
lcost = S.count('E')
if S[0] == 'E':
    lcost -= 1
ans = lcost

for i in range(N-1):
        if S[i] == 'W':
            rcost += 1
        if S[i+1] == 'E':
            lcost -= 1
        ans = min(ans, rcost+lcost)
    
print(ans)

Solution (Python):
N = int(input())
S = input()

w, e = [0], [0]
for n in range(N):
	if "W" == S[n]:
		w += [w[-1] + 1]
	else:
		w += [w[-1]]
	if "E" == S[-n - 1]:
		e += [e[-1] + 1]
	else:
		e += [e[-1]]
w = w[:-1]
e = e[:-1][::-1]




min_dir = 10 ** 12
for n in range(N):
	min_dir = min(min_dir, abs(e[n] + w[n]))

print(min_dir)

Solution (Python):
n = int(input())
S = [s for s in input()]
e,w = [0],[0]
for i in range(n):
    if S[i] is 'W': e.append(e[i]+1)
    else:   e.append(e[i])
    if S[n-i-1] is 'E': w.append(w[i]+1)
    else:   w.append(w[i])
w = w[::-1]
ans = float('inf')
for i in range(n+1):
    ans = min(ans,e[i]+w[i])
print(ans)

Solution (Python):
N = int(input())
S = input()
E = 'E'
W = 'W'

e = S.count(E)
w = N - e

pre_l = ''
ans = e
t = e
for l in S:
	if l == E:
		t -= 1
	if pre_l == W:
		t += 1
	pre_l = l
	ans = min([ans, t])

print(ans)

Solution (Python):
n=int(input())
s=input()
cnt=s[1:].count('E')
mi=cnt
for i in range(1,n):
  if s[i]=='E':
    cnt-=1
  if s[i-1]=='W':
    cnt+=1
  mi=min(mi,cnt)
print(mi)

Solution (Python):
N = int(input())
S = input()

dw = [0]*N
de = [0]*N

for i in range(N):
  dw[i]= (dw[i-1] if i>0 else 0 )+ (1 if (S[i]=="W") else 0 )
for i in range(N-1,-1,-1):
  de[i]= (de[i+1] if i<N-1 else 0 )+ (1 if (S[i]=="E") else 0 )




m = 100000000000000
for i in range(N):
  dm = 0
  if (i>0):
    dm += dw[i-1]
  if (i<N-1):
    dm += de[i+1]
  m = min(m,dm)
print ( m )

Solution (Python):
N = int(input())
S = input()

W = [0] * N
E = [0] * N

W[0] += S[0] == 'W'
E[-1] += S[-1] == 'E'

for i in range(1, N):
    W[i] = W[i - 1] + (S[i] == 'W')
for i in range(N - 1, 0, -1):
    E[i - 1] = E[i] + (S[i - 1] == 'E')

ans = min(E[1], W[-2])
for i in range(1, N - 1):
    ans = min(ans, W[i - 1] + E[i + 1])
print(ans)

Solution (Python):
n=int(input())
w=input()
ans=[0 for i in range(n)]
for i in range(1,n):
  if w[i]=="E":
    ans[0]+=1
for i in range(1,n):
  ans[i]=ans[i-1]
  if w[i]=="E":
    ans[i]-=1
  if w[i-1]=="W":
    ans[i]+=1
print(min(ans))

Solution (Python):
n = int(input())
s = input()
e = s.count("E")
w = s.count("W")
cnt1 = 0
cnt2 = 0
ans = 10e9
for i in s:
    if i=="E":
        cnt1 += 1
        ans = min(ans,cnt2+e-cnt1)
    else:
        ans = min(ans,cnt2+e-cnt1)
        cnt2 += 1
print(ans)

Solution (Python):
n=int(input())
s=input()
e,w=s.count('E'),s.count('W')
le,lw=0,0
ans=e
for c in s:
	if c=='E':
		e-=1
		ans=min(ans,e+lw)		
	else:
		lw+=1
print(ans)

Solution (Python):
N = int(input())
S = input()

wc = [0, 0]
ec = [S.count("E")]

lc = wc[-1] + ec[-1]
for s in S:
	wc += [wc[-1] + ("W" == s)]
	ec += [ec[-1] - ("E" == s)]
	
	lc = min(lc, wc[-1] + ec[-1])



print(lc)

Solution (Python):
N=int(input())
S=input()
L=[]
w=0
e_count=0
ans=N

for i in range(N):
    L.append(w)
    if S[i]=='W':
        w+=1

for i in range(N):
    ans=min(L[N-i-1]+e_count,ans)
    if S[N-i-1]=='E':
            e_count+=1

print(ans)

Solution (Python):
n = int(input())
s = input()
ans = 10**9
l = 0
r = 0
L = [0]
R = [0]
for i in range(n - 1):
    l += s[i].count('W')
    r += s[-i - 1].count('E')
    L.append(l)
    R.append(r)
for l, r in zip(L, R[::-1]):
    ans = min(ans, l + r)
print(ans)

Solution (Python):
N = int(input())
S = str(input())
count = [S[1:].count('E')]
for i in range(1, N):
	add = 0
	if S[i] == 'E':
		add -= 1
	if S[i-1] == 'W':
		add += 1
	count.append(count[i-1]+add)
print(min(count))

Solution (Python):
n=int(input()) 
member=input()

left_w=[0]*n
right_e=[0]*n
for i in range(1,n):
    left_w[i]=left_w[i-1]
    if member[i-1]=="W":
        left_w[i]+=1
    
    right_e[n-i-1]=right_e[n-i]
    if member[n-i]=="E":
        right_e[n-i-1]+=1
    
min_num=n
for i in range(n):
    
    min_num=min(min_num,left_w[i]+right_e[i])
print(min_num)

Solution (Python):
N = int(input())
S = input()
E = []
W = []
k = 0
for i in range(N):
  if S[i] == 'E':
    k += 1
  E.append(k)
l = 0
for i in range(N):
  if S[N-i-1] == 'W':
    l += 1
  W.append(l)
W.reverse()
ME = E[N-1]
MW = W[0]
ans = float('inf')
for i in range(N):
  if ans >= ME-E[i]+MW-W[i]:
    ans = ME-E[i]+MW-W[i]
print(ans)

Solution (Python):
N=int(input())
S = input()
from itertools import accumulate
x = list(accumulate([1 if c=="E" else 0 for c in S]))
if S[0] == "E":
    cnt = N-x[N-1]
else:
    cnt = x[N-1]
if S[N-1] == "E":
    cnt = min(cnt, N-x[N-1])
else:
    cnt = min(cnt, x[N-1])
for i in range(1,N-1):
    if S[i] == "E":
        left = i-x[i-1]
        right = x[N-1]-x[i]
    else:
        left = x[i-1]
        right = N-x[i+1]
    cnt = min(cnt, left+right)
print(cnt)

Solution (Python):
N = int(input())
S = input()

East = [0] * N
West = [0] * N

for i,s in enumerate(list(S)):
    if s == 'W' : 
        if i==0:
            East[i] = 1
        else:
            East[i] = East[i-1] + 1
    else:
        if i == 0:
            East[i] = 0
        else:
            East[i] = East[i-1]
i = N-1
for s in reversed((list(S))):
    if s == 'E' : 
        if i==N-1:
            West[i] = 1
        else:
            West[i] = West[i+1] + 1
    else:
        if i == N-1:
            West[i] = 0
        else:
            West[i] = West[i+1]
    i -= 1
            
ans = 2**31-1

for i in range(N):
    if i == 1:
        ans2 = West[1]
    elif i == N-1:
        ans2 = East[N-2]
    else:
        ans2 = East[i-1]+West[i+1]
    ans = min(ans,ans2)
    
print(ans)

Solution (Python):
import itertools

N = int(input())
S = input()
hidarikara = [0]
for i,c in enumerate(S):
    if S[i] == "W":
         hidarikara.append( hidarikara[i] + 1  )
    else:
        hidarikara.append(hidarikara[i])
S = S[::-1]

migikara = [0]

for i,c in enumerate(S):
    if S[i] == "E":
         migikara.append( migikara[i] + 1  )
    else:
        migikara.append(migikara[i])
anss = [migikara[i] + hidarikara[N-1-i] for i in range(N)]
print(min(anss))

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

count_w_before = []
counter = 0
for i in range(n):
    if s[i] == 'W':
        counter += 1
    count_w_before.append(counter)

count_e_after = []
counter = 0
for i in range(n):
    if s[-(i+1)] == 'E':
        counter += 1
    count_e_after.append(counter)
count_e_after = count_e_after[::-1]

counter = [w + e for (w, e) in zip(count_w_before, count_e_after)]
print(min(counter) - 1)

Solution (Python):
N = int(input())
S = input()
left = 1 if S[0] == 'W' else 0
right = S[1:].count('E')
MIN =  right 
for i in range(1,N):
  if S[i] == 'W':
    MIN = min(MIN,left+right)
    left += 1
  else:
    MIN = min(MIN,left+right)
    right -= 1
  
  if i==N-1:
    MIN = min(MIN,S[:i].count('W'))

print(MIN)

Solution (Python):
N = int(input())
S = input()
dpe = [0] * (N+1)
dper = [0] * (N+1)
for i in range(N):
    dpe[i+1] = dpe[i]
    dper[N-1-i] = dper[N-i]
    if S[i] == "W":
        dpe[i+1] += 1
    if S[N-1-i] == "E":
        dper[N-1-i] += 1
ans = 10**6
for i in range(N):
    tmpl = dpe[i]
    tmp = dpe[i] + dper[i+1]

    ans = min(ans, tmp)
print(ans)

Solution (Python):
n = int(input())
s = list(input())
esum = s.count('E')
mi = esum
for i in range(n):
    if(s[i])=='W':
        esum += 1
    else:
        esum -= 1
    if esum < mi:
        mi = esum
print(mi)

Solution (Python):
N = int(input())
S = input()
E,W = [0]*(N+1), [0]*(N+1)
ans = 10 ** 9

for i in range(N):
    E[i+1], W[i+1] = E[i], W[i]
    if S[i] == 'E':
        E[i+1] += 1
    else:
        W[i+1] += 1

for j in range(N):
    tmp = E[N]-E[j+1]+W[j]
    ans = min(ans, tmp)

print(ans)

Solution (Python):
N = int(input())
S = input()
left = S.count("E")
right = 0
ans = left

for i in S:
    if i == "E":
        left -= 1

    ans = min(ans, left + right)

    if i == "W":
        right += 1

print(ans)

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

left_ws = [0]
for c in s[:-1]:
    plus = 1 if c=='W' else 0
    left_ws.append(left_ws[-1] + plus)

right_es = [sum(1 for c in s[1:] if c=='E')]
for c in s[1:]:
    minus = 1 if c=='E' else 0
    right_es.append(right_es[-1] - minus)

print(min(ws+es for ws, es in zip(left_ws, right_es)))

Solution (Python):
import sys
input = sys.stdin.readline
N = int(input())
S = list(input())

e, w = 0, 0
right, left = [], []
for s in S:
    if s == 'E':
        e += 1
    else:
        w += 1
    left.append([e, w])
ss = list(reversed(S))
e, w = 0, 0
for s in ss:
    if s == 'E':
        e += 1
    else:
        w += 1
    right.append([e, w])
right.reverse()
ans = float('inf')

for i in range(N):
    if i == 0:
        ans = min(ans, right[1][0])
    elif i == N - 1:
        ans = min(ans, left[-2][1])
    else:
        ans = min(ans, right[i + 1][0] + left[i - 1][1])
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
N = int(input())
S = list(input())

e, w = 0, 0
right, left = [], []
for s in S:
    if s == 'E':
        e += 1
    else:
        w += 1
    left.append([e, w])
ss = list(reversed(S))
e, w = 0, 0
for s in ss:
    if s == 'E':
        e += 1
    else:
        w += 1
    right.append([e, w])
right.reverse()
ans = 10**6

for i in range(N):
    if i == 0:
        ans = min(ans, right[1][0])
    elif i == N - 1:
        ans = min(ans, left[-2][1])
    else:
        ans = min(ans, right[i + 1][0] + left[i - 1][1])
print(ans)

Solution (Python):
N = int(input())
S = list(input())

e, w = 0, 0
right, left = [], []
for s in S:
    if s == 'E':
        e += 1
    else:
        w += 1
    left.append([e, w])
ss = list(reversed(S))
e, w = 0, 0
for s in ss:
    if s == 'E':
        e += 1
    else:
        w += 1
    right.append([e, w])
right.reverse()
ans = 10**6

for i in range(N):
    if i == 0:
        ans = min(ans, right[1][0])
    elif i == N - 1:
        ans = min(ans, left[-2][1])
    else:
        ans = min(ans, right[i + 1][0] + left[i - 1][1])
print(ans)

Solution (Python):
N = int(input())
S = input()

east_count = [0]*(N+1)
west_count = [0]*(N+1)

for i in range(N):
    east_count[i+1] += east_count[i]
    west_count[i+1] += west_count[i]
    if S[i] == 'E':
        east_count[i+1] += 1
    else:
        west_count[i+1] += 1

ans = 2**64
for i in range(N):
    s = S[i]
    count = west_count[i]
    count += east_count[N] - east_count[i+1]
    ans = min(ans, count)

print(ans)

Solution (Python):
n = int(input())
s = input()
e = [0]
w = [0]
for i in range(n):
    e.append(e[i] + (s[i] == 'E'))
    w.append(w[i] + (s[i] == 'W'))
e.append(e[-1])
w.append(w[-1])
ans = 10**10

for i in range(1, n+1):
    ans = min(ans, e[-1] - e[i] + w[i - 1])
print(ans)

Solution (Python):
n = int(input())
s = list(input())

ruisekiwa = [0]
ans = n

for i in s:
    if i == "W":
        ruisekiwa.append(ruisekiwa[-1]+1)
    else:
        ruisekiwa.append(ruisekiwa[-1])
        
for i in range(1,n+1):
    w = ruisekiwa[i-1]
    e = n-i-(ruisekiwa[-1]-ruisekiwa[i])
    ans = min(ans,w+e)
    
print(ans)

Solution (Python):
N = int(input())
S = list(input())

l = [0]*N
r = [0]*N
l[0] = 1 if S[0] == 'W' else 0
r[-1] = 1 if S[-1] == 'E' else 0

for i in range(1,N):
    j = -(i+1)
    l[i] = l[i-1] + (1 if S[i] == 'W' else 0)
    r[j] = r[j+1] + (1 if S[j] == 'E' else 0)

l.insert(0,0)
l.append(0)
r.insert(0,0)
r.append(0)

o = N
for i in range(N):
    o = min(o,l[i]+r[i+2])

print(o)

Solution (Python):
N = int(input())
S = input()
L = []
L.append(0)
for i in range(N):
  if S[i] == 'W':
    L.append(L[i] + 1)
  else:
    L.append(L[i])
R = []
R.append(0)
for i in range(N):
  if S[N-1-i] == 'E':
    R.append(R[i] + 1)
  else:
    R.append(R[i])


M = []
for i in range(N):
  M.append(L[i] + R[N-i-1])
print(min(M))

Solution (Python):
N=int(input())
S=input()
A=[0]*(N+1)
B=[0]*(N+1)
for i in range(N):
    if S[i]=="W":
        A[i+1]=A[i]+1
    else:
        A[i+1]=A[i]
for i in range(N,0,-1):
    if S[i-1]=="E":
        B[i-1]=B[i]+1
    else:
        B[i-1]=B[i]
ans=10**19
for i in range(N):
    ans=min(ans,A[i]+B[i+1])
print(ans)

Solution (Python):
N = int(input())
S = input()
dp = [0] * N
for s in S[1:]:
    if s == 'E':
        dp[0] += 1
for i in range(1, N):
    dp[i] = dp[i-1]
    if S[i] == 'E':
        dp[i] -= 1
    if S[i-1] == 'W':
        dp[i] += 1
print(min(dp))

Solution (Python):
N = int(input())
S = input()

e = S.count("E")
w = 0
res = e
for i, s in enumerate(S):
    if s == "E":
        e -= 1
    tmp = e + w
    res = tmp if tmp < res else res
    if s == "W":
        w += 1
print(res)

Solution (Python):
N = int(input())
S = input()
li_E = [0]
li_W = [0]
cost = 10**8
num = 0

for i in S:
    if(i=='E'):
        li_E+=[1+li_E[-1]]
        li_W+=[li_W[-1]]
    else:
        li_E+=[li_E[-1]]
        li_W+=[1+li_W[-1]]
for i in range(1,N+1):
    c = 10**8
    c=li_W[i-1]+li_E[-1]-li_E[i]
    if(cost>c):
        cost = c
        num = i

print(cost)

Solution (Python):
n=int(input())
s=input()
ans=n
sl=[0]
sr=[0]
c=0
for i in s:
    if i=="W":c+=1
    sl.append(c)
s=s[::-1]
c=0
for i in s:
    if i=="E":c+=1
    sr.append(c)
sr=sr[::-1]
for i in range(n):ans=min(ans,sl[i]+sr[i+1])
print(ans)

Solution (Python):
from statistics import mean, median,variance,stdev
import numpy as np
import sys
import math
import fractions
import itertools
import copy
import collections
from operator import itemgetter

def j(q):
    if q==1: print("You can win")
    else:print("You will lose")
    exit(0)


def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")

def ip():
    return int(input())
def printrow(a):
    for i in range(len(a)):
        print(a[i])
def combinations(n,r):
    if n<r:return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
def permutations(n,r):
    if n<r:return 0
    return math.factorial(n) // math.factorial(n - r)
n = ip()                                     


a = input()                                  






righte = 0
rightw = 0
leftw = 0
lefte = 0
for i in range(n):
    if a[i] == 'E':
        righte+=1
    else: rightw+=1
s = n
for i in range(n):
    if a[i] == 'E':
        righte-=1
    else:
        rightw-=1
    s = min(s,leftw+righte)
    if a[i] == 'E':
        lefte+=1
    else:
        leftw+=1
print(s)

Solution (Python):
N = int(input())
S = list(input())
vW = [0] * (N+1)
vE = [0] * (N+1)

for i in range(N):
    vW[i+1] += vW[i]
    if S[i] == "W":
        vW[i+1] += 1
    vE[i+1] += vE[i]
    if S[i] == "E":
        vE[i+1] += 1

ans = float("inf")
for i in range(N):
    ans = min(vW[i] + (vE[N]-vE[i+1]), ans)

print(ans)

Solution (Python):
n=int(input())
S=input()
w_t=S.count("W")
w_c=0
ans=float("inf")
for i in range(n):
  if S[i]=="W":
    t=1
  else:
    t=0
  ans=min(ans,w_c+n-i-1-(w_t-w_c-t))
  if S[i]=="W":
    w_c+=1
print(ans)

Solution (Python):
N = int(input())
S = input()
east = [0]*N
west = [0]*N

tmp1 = 0
for i in range(N-1,-1,-1):
    east[i] = tmp1
    if S[i] == "E":
        tmp1 += 1

tmp2 = 0
for i in range(N):
    west[i] = tmp2
    if S[i] == "W":
        tmp2 += 1

ans = N
for e, w in zip(east,west):
    if ans > e + w:
        ans = e + w
print(ans)

Solution (Python):
n=int(input())
s=input()
ans=300000
l=[0]
r=[0]
for i in s:
  if i=="W":
    l.append(1)
    r.append(0)
  else:
    l.append(0)
    r.append(1)
l.append(0)
r.append(0)
for i in range(1,n+2):
  l[i]+=l[i-1]
for i in range(n,-1,-1):
  r[i]+=r[i+1]
for i in range(1,n+1):
  ans=min(ans,l[i-1]+r[i+1])
print(ans)

Solution (Python):
N = int(input())
S = input()

ct_sum = []
ct_E, ct_W = 0, 0
for i in S:
    if i == 'E':
        ct_E += 1
    else:
        ct_W += 1
    ct_sum.append((ct_E, ct_W))

Max = 0
for i in range(N):
    if i == 0:
        E_l = 0
    else:
        E_l = ct_sum[i-1][0]
    W_i, W_e = ct_sum[i][1], ct_sum[N-1][1]
    W_r = W_e - W_i
    ct = E_l + W_r
    Max = max(Max, ct)

ans = N-1 - Max

print(ans)

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

def f():
    ans,total = 0,0
    for s in S:
        if s == 'W':
            total += 1
        elif total:
            total -= 1
            ans += 1
        else:
            pass
    return ans

print(f())

Solution (Python):
N = int(input())
S = input()

wnums = [0]
enums = [0]
for i in range(0, N-1):
    if S[i] == 'W':
        wnums.append(wnums[i]+1)
    else:
        wnums.append(wnums[i])

j=0
for i in range(N-1, 0, -1):
    if S[i] == 'E':
        enums.append(enums[j]+1)
    else:
        enums.append(enums[j])
    j += 1

ans = 3*10**5
num = 0
for i in range(N):
    num = wnums[i] + enums[N-i-1]
    if ans > num:
        ans = num
    num = 0
print(ans)

Solution (Python):
N = int(input())
S = list(input())
w = [0 for i in range(N)]
e = [0 for i in range(N)]

if S[0] == 'W':
    w[0] = 1

for i in range(1,N-1):
    if S[i] == 'W':
        w[i] = w[i-1] + 1
        e[i+1] = e[i]  
    else:
        w[i] = w[i-1]
        e[i+1] = e[i] - 1

if S[-1] == 'W':
    w[-1] = w[-2] + 1
    scale = -e[-1]
else:
    w[-1] = w[-2]
    scale = -e[-1] +1

ans = w[0] + e[0]
for i in range(N):
    w[i] = w[i] + e[i]
    if ans > w[i]:
        ans = w[i]

print(ans + scale - 1)

Solution (Python):
n = int(input())
s = input()
west = [ 0 for i in range(n+1) ]
east = [ 0 for i in range(n+1) ]
for i in range(1,n+1):
    west[i] = west[i-1]
    east[i] = east[i-1]
    if s[i-1] == 'W':
        west[i] += 1
    if s[i-1] == 'E':
        east[i] += 1

ans = 1e9
for i in range(1,n+1):
    reqd_east_cnt = i-1
    got_east_cnt = east[i-1]
    reqd_west_cnt = n - i
    got_west_cnt = west[n] - west[i]
    
    ans = min( ans, reqd_east_cnt - got_east_cnt + reqd_west_cnt - got_west_cnt)
print(ans)

Solution (Python):
N=int(input())
S=input()
count=S[1:].count('E')
ans=S[1:].count('E')
for i in range(N-1):
  count+=(S[i].count('W')-S[i+1].count('E'))
  ans=count if count<ans else ans
print(ans)

Solution (Python):
N = int(input())
S = input()

W = [0] * N
E = [0] * N
for i in range(1, N):
    if S[i-1] == 'W':
        W[i] = W[i-1] + 1
    else:
        W[i] = W[i-1]

    if S[N-i] == 'E':
        E[N-i-1] = E[N-i] + 1
    else:
        E[N-i-1] = E[N-i]

count = N
for i in range(N):
    count = min(count, W[i] + E[i])
print(count)

Solution (Python):
from sys import stdin
n = int(stdin.readline().rstrip())
s = list(stdin.readline().rstrip())
wl = [0]
el = [0]
ans=[]
for i in range(n):
    wl.append(wl[i]+1 if s[i] == 'W' else wl[i])
for i in range(n):
    el.append(el[i]+1 if s[i] == 'E' else el[i])
for i in range(n+1):
    ans.append(wl[i]+el[-1]-el[i])
print(min(ans))

Solution (Python):
N = int(input())
S = list(input())
W_num = [] 
w_num = 0

for i in range(N):
  W_num.append(w_num)
  if(S[i] == 'W'):
     w_num += 1
     
e_num = 0 
min_cost = float('inf')
for i in range(N):
   j = N-1-i
   tmp = W_num[j] + e_num
   min_cost = min(tmp,min_cost)
   if(S[j] == 'E'):
     e_num += 1
 
print(min_cost)

Solution (Python):
from sys import stdin
import numpy as np

N = int(stdin.readline().rstrip().split()[0])


S = stdin.readline().rstrip().split()[0]

is_w_list = [1 if s=="W" else 0 for s in S ]
cumsum_w = np.append(np.array([0]),(np.cumsum(is_w_list)[:-1])) 

is_e_list = [not(x) for x in is_w_list[::-1]]
cumsum_e = np.append(np.array([0]),(np.cumsum(is_e_list)[:-1]))[::-1]

print(np.min(cumsum_w+cumsum_e))

Solution (Python):
""" AtCoder """

N = int(input())
S = input()

e = S.count('E')
w = 0
ans = e

for s in S:
    ans = min(ans, w + e)
    if s == 'W':
        w += 1
    else:
        e -= 1

ans = min(ans, w + e)

print(ans)

Solution (Python):
N = int(input())
S = input()
Edirect = [0]
numE = 0
ans = N

for i in range(N):
    if S[i] == "E":
        numE += 1
    Edirect.append(numE)

for i in range(1, N+1):
    tmp = ((i-1)-Edirect[i-1]) + (Edirect[N]-Edirect[i])
    if tmp < ans:
        ans = tmp

print(ans)

Solution (Python):
from numpy import*;n=int(input());s=input();print(min(array(cumsum([0]+[(i=="W")for i in s][:n-1]))+s.count("E")-array(cumsum([i=="E"for i in s]))))

Solution (Python):
from numpy import*
n=int(input())
s=input()
x=[0]+[1 if i=="W" else 0 for i in s]
del x[-1]
y=[1 if i=="E" else 0 for i in s]
print(min(array(cumsum(x))+s.count("E")-array(cumsum(y))))

Solution (Python):
N = int(input())
S = input()
a = []
first = 0
for k in range(1, N):
  if S[k] == 'E':
    first += 1

a.append(first)
for k in range(1, N):
  a.append(a[-1] + (S[k-1] == 'W') - (S[k] == 'E'))
print(min(a))

Solution (Python):
N = int(input())
S = input()
t = S.count("E")

mi = N
for i in range(N):
    if S[i] == "E":
        t -= 1
    
    mi = min(mi, t)
    
    if S[i] == "W":
        t += 1
    
print(mi)

Solution (Python):
N=int(input())
s=input()
e=0
w=0
for i in s:
    if(i=='W'):
        w+=1
    elif(i=='E'):
        e+=1
ans=[0,0]
dp=[-1 for _ in range(N)]

for i in range(N):
    tmp=ans[1]
    if(s[i]=='W'):
        ans[1]+=1
    else:
        ans[0]+=1
    dp[i]=tmp+(e-ans[0])

print(min(dp))

Solution (Python):
def solve(string):
    
    length = len(string)
    east, west = [0] * (length + 1), [0] * (length + 2)

    for i in range(1, length + 1):
        if string[i - 1] == "W":
            east[i] = east[i - 1] + 1
        else:
            east[i] = east[i - 1]
    for j in range(length, 0, -1):
        if string[j - 1] == "E":
            west[j] = west[j + 1] + 1
        else:
            west[j] = west[j + 1]

    ans = length
    for k in range(1, length + 1):
        ans = min(ans, east[k - 1] + west[k + 1])
    return ans


def main():
    n = int(input())
    string = input()
    ans = solve(string)
    print(ans)


main()

Solution (Python):
n=int(input())
s=list(input())
p=[0,0]
l=[s.count("E"),s.count("W")]
t=[]
for i in range(n):
    if s[i]=="E":
        t.append(l[0]+p[1]-1)
        l[0]-=1
        p[0]+=1
    else:
        t.append(l[0]+p[1])
        l[1]-=1
        p[1]+=1
print(min(t))

Solution (Python):


import sys


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

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


n = II()
s = SI()

W = [0]
E = [0]
for c in s:
    W.append(W[-1] + (c == 'W'))
    E.append(E[-1] + (c == 'E'))

e_max = E[-1]
ans = INF
for i in range(1, n + 1):
    ans = min(ans, W[i - 1] + e_max - E[i])

print(ans)

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

ans = 0
count = 0

for i in s:
    if i == 'W':
        count += 1
    elif count:
        count -= 1
        ans += 1
    else:
        pass


print(ans)

Solution (Python):
from collections import Counter


def solve(N, S):
    c = Counter(S)
    ans = 3 * 10 ** 5 + 1

    right_e = c["E"]
    left_w = 0
    for i in range(N):
        if S[i] == "E":
            right_e -= 1

        ans = min(ans, left_w + right_e)

        if S[i] == "W":
            left_w += 1
    return ans


if __name__ == "__main__":
    N = int(input())
    S = input()
    print(solve(N, S))

Solution (Python):
n = int(input())
s = input()
ans = len(s)
wc = 0
ec = s.count('E')
for i in range(len(s)):
  if (s[i] == 'E'):
    ec -= 1
  if (ans > ec + wc):
    ans = ec + wc
  if (s[i] == 'W'):
    wc += 1
print(ans)

Solution (Python):
a = int(input())
ar = list(input())
wc = 0
ec = ar.count("E")
br = []
for i in range(a):
    if ar[i] == "E":
        ec -= 1
    br.append(ec + wc)
    if ar[i] == "W":
        wc += 1
print(min(br))

Solution (Python):
N = int(input())
S = input()
count_E = 0
min_change = 0
change = 0
W_flag = True
if S[0] == 'E':
    W_flag = False
for i in range(1, len(S)):
    if W_flag:
        change += 1
    if S[i] == 'E':
        count_E += 1
        W_flag = False
        change -= 1
        min_change = min(change, min_change)
    elif S[i] == 'W':
        W_flag = True
ans = count_E + min_change
print(ans)

Solution (Python):
N = int(input())
S = list(input())

xx = S.count("E")
if S[0] == "E":
	x = xx - 1
else:
	x = xx

ans = x
v = x
for i in range(N - 1):
	if S[i + 1] == "E" and S[i] == "E":
		v -= 1
	elif S[i + 1] == "W" and S[i] == "W":
		v += 1
	else:
		pass

	if v < ans:
		ans = v

print(ans)

Solution (Python):
import numpy
N = input()
S = raw_input()
acc_sum_w2e = numpy.cumsum([1 if c == 'W' else 0 for c in S])
acc_sum_e2w = numpy.cumsum([1 if c == 'E' else 0 for c in S[::-1]])[::-1]
counts = [acc_sum_w2e[i] + acc_sum_e2w[i] - 1 for i in range(N)]
print min(counts)

Solution (Python):
from collections import deque
N=int(input())
S=input()
w=[0]
e=[0]
a=0
b=0
c=N
for i in range(len(S)-1):
    if S[i]=="W":
        a+=1   
    w.append(a)
    if S[-(i+1)]=="E":
        b+=1
    e.append(b)
for i in range(N):
    d=w[i]+e[-(i+1)]
    if d<c:
        c=d
print(c)

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

cnt = 0

ans = []

tmp = 0
now = 1 if s[cnt] == "W" else 0
for i in range(n-1):
    if s[i+1] == "E":
        tmp += 1
ans.append(tmp)

for i in range(n-1):
    tmp += now
    cnt += 1
    now = 1 if s[cnt] == "W" else 0
    tmp += now -1
    ans.append(tmp)
print(min(ans))

Solution (Python):
N=int(input().strip())
S=input().strip()
inf=10**8
ecr=0
for i in range(N):
    if S[i]=='E':
        ecr+=1
ans=inf
wcl=0
for i in range(len(S)):
    if i==0:
        if S[i]=='E':
            ecr-=1
        spre=S[i]
        ans=min(ans,ecr+wcl)
    else:
        if S[i]=='E':
            ecr-=1
        if spre=='W':
            wcl+=1
        spre=S[i]
        ans=min(ans,ecr+wcl)
print(ans)