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):
n = int(input())
s = input()

c = list(s)

lw = 0
le = 0
rw = s[1:].count("W")
re = s[1:].count("E")

ans = lw + re

for i in range(1,n):
    lw = lw + bool(c[i-1] == "W")
    le = le + bool(c[i-1] == "E")
    rw = rw - bool(c[i] == "W")
    re = re - bool(c[i] == "E")
    
    ans = min(ans, lw+re)
    
print(ans)

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

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

a=0
b=0
e=[]
w=[0]
ans=[]

for i in s:
    if i==("E"):
        a+=1
    else:
        b+=1
    e.append(a)
    w.append(b)
        
for i in range(n):
    ans.append(e[n-1]-e[i]+w[i])

print(min(ans))

Solution (Python):
N = int(input())
S = input()
num = S[1:].count('E')
anss = []
anss.append(num)
for i in range(1,len(S)):
  if S[i] == 'E' and S[i - 1] == 'E':
    num -= 1
    anss.append(num)
  elif S[i] == 'W' and S[i - 1] == 'W':
    num += 1
    anss.append(num)
print(min(anss))

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

A = []

for n in range(N):
    if n == 0:
        L = 0
        R = sum([s == 'E' for s in S[n+1:]])
    else:
        L += 0 if S[n-1] == 'E' else 1
        R -= 0 if S[n]   != 'E' else 1
    A.append(L+R)

print(min(A))

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

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

Solution (Python):
N = int(input())
S = input()
s = []
cnt = 0
for i in range(N):
    if S[i] == "W":
        s.append(-1)
    elif S[i] == "E":
        s.append(1)
        if i != 0:
            cnt += 1
        
DP = [cnt]

for i in range(1, N):
    cnt = 0
    if s[i - 1] == -1:
        cnt += 1
    if s[i] == 1:
        cnt -= 1
    DP.append(DP[i - 1] + cnt)

print(min(DP))

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

Solution (Python):
import numpy as np
N = int(input())
S = list(input())

Wleft = np.zeros(N, dtype = int)
Eright = np.zeros(N, dtype = int)
Eright[0] = (S.count("E") if S[0] == "W" else S.count("E") - 1)
for i in range(N-1):
    if S[i] == "W":
        Wleft[i+1] = Wleft[i]+1
    else:
        Wleft[i+1] = Wleft[i]
    if S[i+1] == "E":
        Eright[i+1] = Eright[i] - 1
    else:
        Eright[i+1] = Eright[i]
total = Wleft + Eright
print(np.min(total))

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

answer = prev = S[1:].count('E')
for i in range(1, N):
    _answer = prev if S[i-1] == 'E' else prev+1
    if S[i] == 'E':
        _answer -= 1
    answer = min(answer, _answer)
    prev = _answer

print(answer)

Solution (Python):
n = int(input())
line = list(input())
cum_sum_w = [0]
cum_sum_e = [0]
count_w = 0
count_e = 0
for i in range(n):
    if line[i] == 'W':
        count_w += 1
    else:
        count_e += 1
    cum_sum_w.append(count_w)
    cum_sum_e.append(count_e)
count_list = []
for i in range(1, n + 1):
    count = 0
    count += cum_sum_w[i - 1]
    count += cum_sum_e[n] - cum_sum_e[i]
    count_list.append(count)
print(min(count_list))

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

"""
ある点から西を全てEに、東を全てWにする際のコストを最小化したい

入力例1)
5
WEEWW

EAST: 21000             東から西にかけてEが何個あるか
WEST: 01112             西から東にかけてWが何個あるか
 SUM: 22112             この配列の最小値が答え
"""

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

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

sum = []
for i in range(n):
    sum.append(east[i]+west[i])

print(min(sum))

Solution (Python):
N = int(input())
S = input()
e,w = S.count('E'),0
ans = e+w
for s in S:
  if s == 'E': e -= 1
  else:        w += 1
  ans = min(ans, e+w)
print(ans)

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

Solution (Python):
import numpy as np
N = int(input())
S = input()
S = '*' + S + '*'
W = [x=='W' for x in S]
E = [x=='E' for x in S]
W = np.cumsum(W)
E = np.cumsum(E[::-1])[::-1]

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

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

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

m = float("inf")
for i in range(N):
    cnt = w[i] + e[i] - 1
    if cnt < m:
        m = cnt
print(m)

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

cost = S[1:].count('E')
min_cost = cost

for i in range(1, len(S)):
  cost = cost + (S[i-1] == 'W') - (S[i] == 'E')
  if cost < min_cost:
    min_cost = cost
print(min_cost)

Solution (Python):

"""
Created on Mon Aug  6 17:34:40 2018

@author: Yuki
"""

N = int(input())
S = input()
res = []

res.append(S[1:].count('E'))
for i in range(1, len(S)):
    if S[i] == 'E':
        tmp = res[-1]-1
        if S[i-1] == 'W':
            tmp += 1
        
    elif S[i] == 'W':
        tmp = res[-1]
        if S[i-1] == 'W':
            tmp += 1
    res.append(tmp)
    

print(min(res))

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
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]


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

def main():
    N = int(input())
    S = input()
    cs = [0] * (N + 1)
    for i in range(N):
        cs[i + 1] = cs[i] + (S[i] == "W")

    ans = INF
    for i in range(N):
        l_w = cs[i]
        l_e = i - l_w
        r_w = cs[N] - cs[i + 1]
        r_e = (N - i - 1) - r_w

        ans = min(ans, l_w + r_e)

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
import numpy as np
N = int(input())
S = input()

F = np.zeros(N, dtype=int)

F[0] = S[1:].count('E')

for i in range(1, len(S)):
    F[i] = F[i-1]
    if S[i-1] == 'W':
        F[i] += 1

    if S[i] == 'E':
        F[i] -= 1

minv = np.amin(F)
print(minv)

Solution (Python):
import numpy as np
N = int(input())
S = str(input())

E = np.cumsum([1 if s == "E" else 0 for s in S])
W = np.cumsum([1 if s == "W" else 0 for s in S])

ans = float("inf")
for i, v in enumerate(S):
    if v == "E":
        ans = min(ans, (E[-1] - E[i]) + W[i])
    else:
        ans = min(ans, (E[-1] - E[i]) + W[i] - 1)
    

print(ans)

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

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

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

ans = 3 * 10 **5

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

Solution (Python):
n = int(input())
s = input()
E = s.count("E")
W = 0
min_num = n
for s_i in s:
    if s_i == "E":
        E -= 1
    else :
        W += 1

    change_man = E + W - bool(s_i == "W")
    min_num = min(min_num, change_man)
print(min_num)

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

left = 0
left_E = 0
right = N-1
right_W = S[1:].count("W")
ans = ((left-left_E) + (right-right_W))

for i in range(1,N):
    left += 1
    if S[i-1] == "E":
        left_E += 1
        
    right -= 1
    if S[i] == "W":
        right_W -= 1
        
    ans = min(ans, (left-left_E) + (right-right_W))
    
print(ans)

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

cumw = [0]
for c in S:
    cumw.append(cumw[-1] + int(c=='W'))

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

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

min_num = 300000
w_west = 0
e_east = s.count("E")
for s_i in s:
    if s_i == "E":
        e_east -= 1
    else:
        w_west += 1
    tmp = w_west + e_east - bool(s_i == "W")
    if tmp < min_num:
        min_num = tmp
print(min_num)

Solution (Python):
N = int(input())
S = input()
sum_s = [0] * N
sum_s[0] = 1 if S[0] == 'W' else 0
for i in range(1, N):
    if S[i] == 'W':
        sum_s[i] = sum_s[i - 1] + 1
    else:
        sum_s[i] = sum_s[i - 1]

lc = 0
rc = (N - 1) - (sum_s[N - 1] - sum_s[0])
ans = lc + rc

for i in range(1, N):
    lc = sum_s[i - 1]
    rc = (N - (i + 1)) - (sum_s[N - 1] - sum_s[i])

    ans = min(ans, lc + rc)

print(ans)

Solution (Python):
import sys


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

w = [0 for i in range(N)]
e = [0 for i in range(N)]

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

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

ans = N


for i in range(N):
	if S[i] =='E':
		total = (w[i]) + (e[N-1]-e[i])
	else:
		total = (w[i]-w[0]) + (e[N-1]-e[i])
	
	ans = min(ans, total)
print(ans)

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

cum_wlist, cum_elist = [], []
cum_w, cum_e = 0, 0

for i in S :
    if i == "W" :
        cum_w += 1
    else :
        cum_e += 1
    cum_wlist.append(cum_w)
    cum_elist.append(cum_e)
    
min_change = N + 1

for i in range(N) :
    if S[i] == "W" :
        min_change = min(min_change, cum_wlist[i] -  1 + cum_elist[N - 1] - cum_elist[i])
    else :
        min_change = min(min_change, cum_wlist[i] + cum_elist[N - 1] - cum_elist[i])

print(min_change)

Solution (Python):

n = int(input())
sarr = input()
L = 'W'
R = 'E'

RtoL = 0
lead = sarr[0]
for s in sarr[1:]:
	if s == R:
		RtoL += 1

LtoR = 0

lowest = RtoL
for s in sarr[1:]:
	if lead == L:
		LtoR += 1
	if s == R:
		RtoL -= 1
	
	tmpres = LtoR + RtoL
	
	if tmpres < lowest:
		lowest = tmpres
	
	lead = s

print(lowest)

Solution (Python):
import sys

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

West = S.count("W")
ans = sys.maxsize
tmp_W = 0

for i in range(0, N):
    tmp_ans = tmp_W

    if(S[i] == "W"):
        tmp_W += 1

    tmp_ans += N - i - 1 - West + tmp_W

    if(tmp_ans < ans):
        ans = tmp_ans

print(ans)

Solution (Python):


n = int(input())
string = input()
ecnt = 0
for i in range(n):
    if string[i] == "E":
        ecnt += 1
wcnt = 0
minv = 10000000000
for i in range(n):
    if string[i] == "E":
        ecnt -= 1
    if i >= 1 and string[i-1] == "W":
        wcnt += 1
    minv = min(minv,wcnt+ecnt)
    
print(minv)

Solution (Python):
n=int(input())
s=list(input())
w_cnt=[0]*(n+1)
e_cnt=[0]*(n+1)
s.insert(0,"G")
for i in range(1,n+1):
    if s[i]=="W":
        w_cnt[i]=w_cnt[i-1]+1
    else:
        w_cnt[i]=w_cnt[i-1]
s.reverse()
s.pop(n)
s.insert(0,"G")
for i in range(1,n+1):
    if s[i]=="E":
        e_cnt[i]=e_cnt[i-1]+1
    else:
        e_cnt[i]=e_cnt[i-1]
ans=float("inf")
for i in range(n):
    temp=w_cnt[i]+e_cnt[n-1-i]
    if temp<=ans:
        ans=temp
print(ans)

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

num_E = s.count("E")
num_W = 0
ans = float("inf")

for i in range(n):
    if s[i] == "W":
        ans=min(ans , num_E + num_W)
        num_W += 1
    else:
        num_E -= 1
        ans=min(ans , num_E + num_W)
    
print(ans)

Solution (Python):
N = int(input())
direction = list(input())
change = [0]*(N+1)

change[0] = direction.count("E")

for i in range(N):
    f_n = 0
    f_p = 0
    if direction[i] == "E":
        f_n = 1
    else:
        f_p = 1
    change[i+1] = change[i] - f_n + f_p

print(min(change))

Solution (Python):
N = int(input())
S = input()
Ecount = S.count('E')
Wcount = 0
min = Ecount
for i in S:
    if i == 'E':
        Ecount -= 1
    else:
        Wcount += 1
    if min > Ecount + Wcount:
        min = Ecount + Wcount
print(min)

Solution (Python):
import numpy
n = int(input())
s = input()
Ws = [0] + [1 if i == "W" else 0 for i in s]
Ws_sum = list(numpy.cumsum(Ws))
cnt_l = []
for i in range(n):
    cnt_l.append(Ws_sum[i]+n-i-1-Ws_sum[-1]+Ws_sum[i+1])
print(min(cnt_l))

Solution (Python):



def get_cum_sum(s, c):
	cum_sum =[]
	cnt = 0
	
	for i in s:
		if i == c:
			cnt += 1
			cum_sum.append(cnt)
		else:
			cum_sum.append(cnt)
			
	return cum_sum
    
	

def main():
    
    N = int(input())
    S = input()
		
    cum_sum_w = get_cum_sum(S, 'W')
    cum_sum_e = get_cum_sum(S, 'E')
    min_cnt = 300000
    
    for i in range(N):
    	if S[i] == 'W':
    		first_total_w = cum_sum_w[i] - 1
    	else:
    		first_total_w = cum_sum_w[i]
    		
    	last_total_e = cum_sum_e[-1] - cum_sum_e[i]
    	
    	
    	
    	if min_cnt > first_total_w + last_total_e:
    		min_cnt = first_total_w + last_total_e
    
    print(min_cnt)
    	
    	
        


if __name__ == '__main__':
    main()

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

Solution (Python):
import sys
import math
import collections
import itertools
import array
import inspect


sys.setrecursionlimit(10000)


def chkprint(*args):
    names = {id(v):k for k,v in inspect.currentframe().f_back.f_locals.items()}
    print(', '.join(names.get(id(arg),'???')+' = '+repr(arg) for arg in args))


def to_bin(x):
    return bin(x)[2:]



dp = None

def main():
    N = int(input())
    S = input()

    ans = S[1:].count("E")
    A = []
    A.append(ans)

    for i in range(1, N):
        if S[i - 1] == "W":
            ans += 1
        if S[i] == "E":
            ans -= 1
        A.append(ans)

    print(min(A))



main()

Solution (Python):


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


W_ruiseki = [0]
E_ruiseki = [0]

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

ans = float("inf")
for i in range(N):
    cnt = 0
    cnt += W_ruiseki[i]
    cnt += E_ruiseki[N] - E_ruiseki[i+1]
    ans = min(ans, cnt)

print(ans)

Solution (Python):


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


cnt = S[1:].count("E")
ans = cnt

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

print(ans)

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

Solution (Python):
from collections import Counter
N = int(input())
S = list(input())
WL = [0]*(N+1)
for i in range(0,N):
    if S[i] == 'W':
        WL[i+1] = WL[i] + 1
    else:
        WL[i+1] = WL[i]
T = S[::-1]
ER = [0]*(N+1)
for i in range(0,N):
    if T[i] == 'E':
        ER[i+1] = ER[i] + 1
    else:
        ER[i+1] = ER[i]
ans = N
for i in range(N):
    ans = min(ans,WL[i]+ER[N-i-1])
print(ans)

Solution (Python):
import numpy as np
N = int(input())
S = input()

numW = [0]*N
numE = [0]*N
tmpW = 0
tmpE = 0
for i,s in enumerate(S):
    if s == 'W':
        tmpW += 1
    else:
        tmpE += 1

    numW[i] = tmpW
    numE[i] = tmpE

npans = np.zeros(N,dtype = int)
npans[0] = numE[-1] - numE[0]
for i in range(1,N):
    npans[i] = numW[i-1] + numE[-1] - numE[i]
print(np.min(npans))

Solution (Python):
n = int(raw_input())
s = raw_input()
left = 0
right = 0
for i in range(1, n):
  if s[i] == 'E':
    right += 1
total = left + right
for i in range(0, n-1):
  if s[i] == 'W':
    left += 1
  if s[i+1] == 'E':
    right -= 1
  if left + right < total:
    total = left + right
print(total)

Solution (Python):
N=int(input())
S=input()
minimum=3*pow(10, 5)
east=[0]
for i in range(1,N+1):
    if S[i-1]=='E':
        east.append(east[i-1]+1)
    else:
        east.append(east[i-1])
for i in range(N):
    candidate = i-east[i]+(east[N]-east[i+1])
    minimum = min(candidate, minimum)
print(minimum)

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

cnt = S[1:].count("E")
ans = cnt
for i in range(1, N):
    if S[i-1] == "W":
        cnt += 1
    if S[i] == "E":
        cnt -= 1
    ans = min(ans, cnt)

print(ans)

Solution (Python):
input()
s = input()
r = s[1:].count("E")
t = r
for i in range(1,len(s)):
  t = t+1 if s[i-1] == "W" else t
  t = t-1 if s[i] == "E" else t
  r = min(r, t)
print(r)

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

ER=[]
WL=[]


w=0

for i in S:
    if i=='W':
        w+=1
    WL.append(w)

e=0

for i in reversed(S):
    if i=='E':
        e+=1
    ER.append(e)
    


result=N

for i in range(N):
    if i==0:
        result=min(result,ER[N-2])
    elif i==(N-1):
        result=min(result,WL[N-2])
    else:
        result=min(result,(ER[N-i-2]+WL[i-1]))
print(result)

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

j=s[1:].count("E")
t=[j]
for i in range(n-1):
	t.append(t[i]+s[i].count("W")-s[i+1].count("E"))
	if j>t[i+1]:
		j=t[i+1]
print(j)

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

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

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

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

w = [s[0].count('W')]
e = [s[0].count('E')]

for i in range(1, n):
    if s[i] == 'W':
        w.append(w[i-1]+1)
        e.append(e[i-1])
    else:
        w.append(w[i-1])
        e.append(e[i-1]+1) 

for i in range(n):
    if i > 0:
        cnt = w[i-1] + e[-1] - e[i]
    else:
        cnt = e[-1] - e[0]
    m = min(cnt, m)
print(m)

Solution (Python):
n = int(input())
s = input()
res = n
W = [1 if s[0] == 'W' else 0] * n
E = [1 if s[0] == 'E' else 0] * n
for i in range(1,n):
    if s[i] == 'W':
        W[i] = W[i-1]+1
        E[i] = E[i-1]
    else:
        W[i] = W[i-1]
        E[i] = E[i-1] + 1
for i in range(n):
    cnt = W[i] + E[-1] - E[i]
    if s[i] == 'W':
        cnt -= 1
    res = min(cnt, res)
print(res)

Solution (Python):
n = int(input())
s = input()
res = n
W = [0] * n
E = [0] * n 
if s[0] == 'W':
    W[0] = 1
    E[0] = 0
else:
    W[0] = 0
    E[0] = 1
for i in range(1,n):
    if s[i] == 'W':
        W[i] = W[i-1]+1
        E[i] = E[i-1]
    else:
        W[i] = W[i-1]
        E[i] = E[i-1] + 1
for i in range(n):
    cnt = W[i] + E[-1] - E[i]
    if s[i] == 'W':
        cnt -= 1
    res = min(cnt, res)
print(res)

Solution (Python):

n = int(input())
s = input()

w = [0]*n 
e = [0]*n 
for i in range(n):
    if s[i] == 'W':
        w[i] = w[i-1] + 1 if i > 0 else 1
    else:
        w[i] = w[i-1] if i > 0 else 0
    
    if s[-i-1] == 'E':
        e[-i-1] = e[-i] + 1 if i < n else 1
    else:
        e[-i-1] = e[-i] if i < n else 0


ans = int(1e6)
for i in range(n):
    if i == 0: a = e[1]
    elif i == n-1: a = w[-2]
    else: a = w[i-1] + e[i+1]
    ans = min(ans, a)
print(ans)

Solution (Python):
n = int(input())
s = list(input())
left_w = 0
right_e = s[1:].count("E")
ans = right_e
for i in range(1,n):
    if s[i-1] == "W":
        left_w += 1
    if s[i] == "E":
        right_e -= 1
    ans = min(ans,right_e +left_w)
print(ans)

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

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

num_W = 0
num_E = 0
for i in range(1,N):
    if S[i] == "E":
        num_E += 1
min_ct = num_W + num_E

for i in range(1,N):
    if S[i-1] == "W":
        num_W += 1
    if S[i] == "E":
        num_E -= 1
    ct = num_W + num_E
    if min_ct > ct:
        min_ct = ct


print(min_ct)

Solution (Python):


def solve(N, S):
    dp = [[0 for i in range(N+1)] for j in range(N+1)]
    
    
    for s in S:
        if s == "W":
            dp[0][N] += 1
    maxv = dp[0][N]
    
    
    for i in range(1, N+1):
        if S[i-1] == "E":
            diff = 1
        else:
            diff = -1
        dp[i][N-i] = dp[i-1][N-i+1] + diff
        if dp[i][N-i] >= maxv:
            maxv = dp[i][N-i]
    print(N - maxv)

def solve2(N, S):
    good = 0
    for s in S:
        if s == "W":
            good += 1
    maxv = good

    nowv = maxv
    for i in range(1, N+1):
        if S[i-1] == "E":
            diff = 1
        else:
            diff = -1
        nowv = nowv + diff
        if nowv >= maxv:
            maxv = nowv
    print(N - maxv)

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

Solution (Python):
N =  int(input())
S = input()
accw = [0]*(N+1)
acce = [0]*(N+1)
for i in range(N):
	if S[i] == 'E':
		acce[i+1] = acce[i]+1
		accw[i+1] = accw[i]
	else:
		accw[i+1] = accw[i]+1
		acce[i+1] = acce[i]


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

print(ans)

Solution (Python):
N = int(input())
S = input()
s1 = S[1:]
b = []
b.append(s1.count('E'))
j = 1
while j < N:
  b.append(0)
  b[j] = b[j-1]
  if S[j] == 'E':
    b[j] += -1
  if S[j-1] == 'W':
    b[j] += 1
  j += 1
print(min(b))

Solution (Python):
if __name__ == '__main__':
    N = int(input())
    S = input()

    numE = S.count('E')
    numW = S.count('W')

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

    sumE = []
    for i in range(N):
        sumE.append(numE-i+sumW[i])
        if S[i] == 'E':
            sumE[i] -= 1
    att = []
    for i in range(N):
        att.append(sumW[i] + sumE[i])

    print(min(att))

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

print(min(ans))

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

lE = [0] * N
lW = [0] * N
le = lw = 0
for i in range(N):
    if S[i] == "E":
        le += 1
    else:
        lw += 1
    lE[i] = le
    lW[i] = lw

rtn = lE[-1] - lE[0]

for i in range(1, N):
    tmp = lW[i-1] + lE[-1] - lE[i]
    if tmp <= rtn:
        rtn = tmp


print(rtn)

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

Solution (Python):
N = int(input())
S = [x[:1] for x in input()]
ans = 0
count = 0

ruiE, ruiW = [], []
ruie,ruiw = 0, 0
for i in range(len(S)):
    if S[i] == "E":
        ruie += 1
    elif S[i] == "W":
        ruiw += 1

    ruiE.append(ruie)
    ruiW.append(ruiw)


def countW(j):
    return ruiW[j]

def countE(i):
    return ruiE[len(S) - 1] - ruiE[i-1]



ans = 0 + countE(1)


for h in range(1, len(S)-1):
    count = countW(h-1) + countE(h+1)

    if count <= ans:
        ans = count



count = countW(len(S)-2) + 0

if count <= ans:
    ans = count

print(ans)

Solution (Python):
N = int(input())
S = [x[:1] for x in input()]
ans = 0
count = 0

ruiE, ruiW = [], []
ruie,ruiw = 0, 0
for i in range(len(S)):
    if S[i] == "E":
        ruie += 1
    elif S[i] == "W":
        ruiw += 1

    ruiE.append(ruie)
    ruiW.append(ruiw)



def countW(j):
    return ruiW[j]

def countE(i):
    return ruiE[len(S) - 1] - ruiE[i-1]





ans = 0 + countE(1)


for h in range(1, len(S)-1):
    count = countW(h-1) + countE(h+1)

    if count <= ans:
        ans = count

   


count = countW(len(S)-2) + 0

if count <= ans:
    ans = count

print(ans)

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

m = N
x = S.count('E')

for i in S:
    if i == 'E':
        x = x - 1

    if x < m:
        m = x

    if i == 'W':
        x = x + 1

print(m)

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

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


dl = {}
dr = {}
acc = 0
for i, s in enumerate(S):
    dl[i] = acc
    if s == 'W':
        acc += 1

acc = 0
for i, s in enumerate(S[::-1]):
    dr[N - i - 1] = acc
    if s == 'E':
        acc += 1

result = N
for i in range(N):
    if result > dl[i] + dr[i]:
        result = dl[i] + dr[i]
print(result)

Solution (Python):
n = int(input())
s = input()
t = sum([c == 'E' for c in s[1:]])
r = t
for i in range(1,n):
    t += (s[i-1] == 'W')
    t -= (s[i] == 'E')
    r = min(r,t)
print(r)

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

tmp_ans = S[1:].count("E")
ans = tmp_ans

for i in range(1, N):
    if S[i-1] == "W":
        tmp_ans += 1
    if S[i] == "E":
        tmp_ans -= 1
    
    if tmp_ans < ans:
        ans = tmp_ans
print(ans)

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

ans = S[1:].count('E')
tmp = ans
for i in range(1, N):
  if S[i] == 'E':
    tmp -= 1
  if S[i-1] == 'W':
    tmp += 1
  if tmp<ans:
    ans=tmp
print(ans)

Solution (Python):
n = int(input())
s = input()
num = [0 for i in range(n)]
w = [0 for i in range(n)]
if s[0] == 'W':
    w[0] = 1
for i in range(1, n):
    if s[i] == 'W':
        w[i] = (w[i-1] + 1)
    else:
        w[i] = w[i-1]
e = [0 for i in range(n)]
if s[n-1] == 'E':
    e[n-1] = 1
for i in range(2, n+1):
    if s[n-i] == 'E':
        e[n-i] = (e[n-i+1] + 1)
    else:
        e[n-i] = e[n-i+1]
sol = [0 for i in range(n)]
sol[0] = e[0]
sol[n-1] = w[n-2]
for i in range(1, n-1):
    sol[i] = w[i-1] + e[i+1]
print(min(sol))

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

csum_w = [0] * n
if s[0] == 'W':
    csum_w[0] = 1
for i in range(1, n):
    cnt = csum_w[i - 1]
    if s[i] == 'W':
        cnt += 1
    csum_w[i] = cnt

ans = float('inf')
for i in range(n):
    buf = 0
    if i >0:
        buf += csum_w[i - 1]
    buf += (n - 1 - i) - (csum_w[n - 1] - csum_w[i])
    ans = min(buf, ans)
print(ans)

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

Solution (Python):
input()
s=input()
temp=s[1:].count("E")
res=temp
for i in range(len(s)-1):
    if s[i+1]=="E":
        temp-=1
    if s[i]=="W":
        temp+=1
    res=min(res,temp)
print(res)

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

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

w_count = [0]
e_count = [0]

a_r = list(reversed(a))

for i in range(1, n+1):
    if a[i-1] == 'W':
        w_count.append(w_count[i-1] + 1)
    else:
        w_count.append(w_count[i-1])
    if a_r[i-1] == 'E':
        e_count.append(e_count[i-1] + 1)
    else:
        e_count.append(e_count[i-1])

w = w_count[1:]
e = list(reversed(e_count[1:]))

sum = 10000000
for i in range(0, n):
    if sum > w[i] + e[i]:
        sum = w[i] + e[i]

print(sum-1)

Solution (Python):
n=int(input())
s=input()
e=[0]
w=[0]
for i, si in enumerate(s):
    if si=='W':
        w.append(w[i]+1)
    else:
        w.append(w[i])
for i, si in enumerate(reversed(s)):
    if si=='E':
        e.append(e[i]+1)        
    else:
        e.append(e[i])

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

Solution (Python):
from itertools import accumulate

n, s = input(), input()

a = accumulate(c=='W' for c in 'X'+s[:-1])
b = reversed(list(accumulate(c=='E' for c in 'X'+s[-1:0:-1])))
print(min(x + y for x, y in zip(a, b)))

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

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




result = 3*10**5

Wcount = 0  
Ecount = s.count("E") 

for n,i in enumerate(s):
    Wflag = False
    if i == "W":
        Wflag = True
    else:
        Ecount -= 1
    if result > Wcount+Ecount:
        result = Wcount+Ecount
    if Wflag:
        Wcount += 1

print(result)

Solution (Python):

"""
C - Attention

"""
import sys
from sys import stdin
input = stdin.readline


def solve(N, S):
    ans = float('inf')
    left_w = -1
    left_e = 0
    right_w = S.count('W')
    right_e = S.count('E')

    prev_ch = 'W'
    for ch in S:
        if prev_ch == 'W':
            left_w += 1
        else:
            left_e += 1
        if ch == 'W':
            right_w -= 1
        else:
            left_w -= 1
        prev_ch = ch
        c = left_w + right_e
        ans = min(ans, c)
    return ans


def main(args):
    N = int(input())
    S = input().strip()
    ans = solve(N, S)
    print(ans)


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

Solution (Python):
n = int(input())
s = input()
e = [0]
w = [s[1:].count('E')]
for i in range(1, n):
  e.append(e[-1]+(s[i-1]=='W'))
  w.append(w[-1]-(s[i]=='E'))
print(min(a+b for a,b in zip(e,w)))