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

result=10**6
hantei=0

num_E=S.count('E')
num_W=S.count('W')

pr_E=0
pr_W=0

for i in range(N):
    if i==0:
        hantei=num_E-(S[i]=='E')*1
        pr_E+=(S[i]=='E')*1
        pr_W+=(S[i]=='W')*1
    elif i==N-1:
        hantei=num_W-(S[i]=='W')*1
    else:
        hantei=pr_W+num_E-(pr_E+(S[i]=='E')*1)
        pr_E+=(S[i]=='E')*1
        pr_W+=(S[i]=='W')*1
    
    result=int(min(result,hantei))

print(result)

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

l = [0 for i in range(n)]
r = [0 for i in range(n)]
for i in range(1, n):
  rev = -(i+1)
  l[i] = l[i-1] + 1 if s[i-1] == "W" else l[i-1]
  r[rev] = r[rev+1] + 1 if s[rev+1] == "E" else r[rev+1]

ans = 1e+9
for i in range(n):
  ans = min(ans, l[i] + r[i])
print(ans)

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

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

W = [0]*n
E = [0]*n

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

ans=float('inf')
for i in range(n):
    ans=min(ans,W[i]+E[i])

print(ans)

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

l = [0]*n
l[0] = s[1:].count("E")

for i in range(1,n):
    if s[i-1] == "W":
        l[i] += 1
    if s[i] == "E":
        l[i] -= 1
    l[i] += l[i-1]

print(min(l))

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

left = [0] * n
right = [0] * n

tmp = 0
for i in range(1, n):
    if s[i - 1] == "W":
        tmp += 1
        left[i] = tmp
    else:
        left[i] = tmp

tmp = 0
for i in range(n - 2, -1, -1):
    if s[i + 1] == "E":
        tmp += 1
        right[i] = tmp
    else:
        right[i] = tmp

res = 10**9
for i in range(n):
    res = min(res, left[i] + right[i])
print(res)

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

line = [0]*(n+1)

for i in range(n):
    if s[i] == 'W':
        line[0] += 1
        line[i+1] -= 1
    if s[i] == 'E':
        line[i] += 1
        line[n] -= 1



for i in range(n-1):
    line[i+1] += line[i]


print(len(s)-max(line))

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

Solution (Python):
n = int(input())
string = input()
es = [0]
ws = [0]
for s in string:
    if s == 'E':
        es.append(es[-1]+1)
        ws.append(ws[-1])
    else:
        es.append(es[-1])
        ws.append(ws[-1]+1)
ans = float('inf')
for i in range(n):
    i += 1
    score = ws[i-1] + es[-1] - es[i]
    ans = min(score, ans)

print(ans)

Solution (Python):
n = int(input())
s = list(input())
w = [0 for _ in range(n+1)]
e = [0 for _ in range(n+1)]

for i in range(n):
    if s[i] == "W":
        w[i+1] = w[i]+1
        e[i+1] = e[i]
    else:
        e[i+1] = e[i]+1
        w[i+1] = w[i]


ans = float('inf')
for i in range(n):
    a = w[i+1]
    b = e[n] - e[i+1]
    if s[i] == 'W':
        a-=1

    ans = min(ans,a+b)
    

print(ans)

Solution (Python):
n=int(input())
s=input()
a=[0]
for i in range(n):
	a.append(a[i]+(s[i]=='W'))
p=n
for i in range(n):
	j=a[i]
	k=n-i-(a[-1]-a[i+1])-1
	
	p=min(p,j+k)
print(p)

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

E_count = [0] * (N+1)
W_count = [0] * (N+1)

for i in range(N):
    E_count[i + 1] = E_count[i] + int(S[i] == "E")
    W_count[i + 1] = (i + 1) - E_count[i + 1]

ans = 10 ** 6
for i in range(1, N + 1):
    change_dir_cnt = (W_count[i - 1] - W_count[0]) + (E_count[-1] - E_count[i])
    ans = min(change_dir_cnt, ans)
print(ans)

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

E_count = [0] * (N+1)
for i in range(N):
    E_count[i+1] = E_count[i] + int(S[i] == "E")

W_count = [0] * (N+1)
for i in range(N):
    W_count[i+1] = W_count[i] + int(S[i] == "W")

ans = 10 ** 6
for i in range(1, N + 1):
    change_dir_cnt = (W_count[i - 1] - W_count[0]) + (E_count[-1] - E_count[i])
    ans = min(change_dir_cnt, ans)
print(ans)

Solution (Python):
import sys
from collections import Counter
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


def main():
    N = int(readline())
    S = readline().decode('utf-8')
    L = len(S)
    A = [0]*L
    for i in range(L):
        if S[i] == 'E':
            if i == 0:
                A[i] = 1
            else:
                A[i]=A[i-1]+1
        else:
            if i == 0:
                continue
            else:
                A[i]=A[i-1]
    ans = INF
    for i in range(N):
        tmp = 0
        if i>0:
            tmp += i - A[i-1]
        if i<N-1:
            tmp += A[N-1]-A[i]
        ans = min(ans,tmp)
    print(ans)






if __name__ == '__main__':
    main()

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

w_l=[0]
s_l=[0]
for c in s:
    if c=='W':
        w_l.append(w_l[-1]+1)
        s_l.append(s_l[-1])
    else:
        w_l.append(w_l[-1])
        s_l.append(s_l[-1]+1)
ans=1145141919
for i in range(n):
    ans=min(ans, w_l[i]+s_l[-1]-s_l[i+1])
print(ans)

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






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

print(ans)

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

Solution (Python):
a = int(input())
ar = input()
n = ar.count("E")
if ar[0] == "E":
    n -= 1
mi = n
for i in range(1,a):
    if ar[i] == "E":
        n -= 1
    if ar[i-1] == "W":
        n += 1
    if n < mi:
        mi = n
print(mi)

Solution (Python):
import sys
import re
import math
import collections
import decimal
import bisect
import itertools
import fractions

import copy





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

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





def main():
    n = ni()
    s = list(input())

    w_in_left_side_of_me = [0 for _ in range(n)]
    e_in_right_side_of_me = [0 for _ in range(n)]

    for i in range(1, n):
        w_in_left_side_of_me[i] = w_in_left_side_of_me[i - 1]
        if s[i - 1] == "W":
            w_in_left_side_of_me[i] += 1

    for i in range(n - 1 - 1, -1,-1):
        e_in_right_side_of_me[i] = e_in_right_side_of_me[i + 1]
        if s[i + 1] == "E":
            e_in_right_side_of_me[i] += 1

    ans = sys.maxsize
    for i in range(n):
        ans = min(ans, w_in_left_side_of_me[i] + e_in_right_side_of_me[i])

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
s=input()
l=[0]*n
east=0
for i in range(n):
    if s[i]=='E':
        east+=1
    l[i]=east

ans=3*10**5
for i in range(n-1,-1,-1):
    if i==n-1:
        R=0
    else:
        R=l[-1]-l[i]
    if i==0:
        L=0
    else:
        L=i-l[i-1]
    ans=min(ans,R+L)
print(ans)

Solution (Python):
import sys

n = int(input())
s = input()
e = [0] * n
w = [0] * (n + 1)
e[0] = 1 if s[0] == 'E' else 0
w[0] = 0
w[1] = 1 if s[0] == 'W' else 0
for i in range(1, n):
    if s[i] == 'E':
        e[i] = e[i - 1] + 1
        w[i + 1] = w[i]
    else:
        e[i] = e[i - 1]
        w[i + 1] = w[i] + 1
min = sys.maxsize
for i in range(n):
    if min > e[-1] - e[i] + w[i]:
        min = e[-1] - e[i] + w[i]
print(min)

Solution (Python):
n = int(input())
s = input()
tmp = s.count('E')
ans = 10**6
for i in range(n):
    if s[i]=='E':
        tmp -= 1
    if i>0 and s[i-1]=='W':
        tmp += 1
    ans = min(ans, tmp)
print(ans)

Solution (Python):
from collections import Counter

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

counts = Counter(s)

cnt_W, cnt_E, ans, cnt = 0, 0, counts['E'], 0
for i in range(n):
    if s[i] == 'E':
        cnt_E += 1
    
    cnt = cnt_W + (counts['E'] - cnt_E)
    ans = min(ans, cnt)
    
    if s[i] == 'W':
        cnt_W += 1

print(ans)

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

Solution (Python):
N = int(input())
easts = [0] * (N + 1)
wests = [0] * (N + 1)

for i, c in enumerate(input()):
    if c == 'E':
        easts[i + 1] += easts[i] + 1
        wests[i + 1] = wests[i]
    else:
        wests[i + 1] += wests[i] + 1
        easts[i + 1] = easts[i]

ans = 1 << 31
for i in range(N):
    ans = min(ans, easts[N] - easts[i + 1] + wests[i] - wests[0])
print(ans)

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


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

ans = 10**9
for i in range(n):
    temp = (w[i] - w[0]) + (e[n] - e[i+1])
    ans = min(ans, temp)

print(ans)

Solution (Python):
N=int(input())
S=input()
DP=[0]*N
l=0
for i,x in enumerate(S):
  DP[i]+=l
  if S[i]=="W":
      l+=1
r=0
for j,y in enumerate(S[::-1]):
  DP[N-1-j]+=r
  if S[N-1-j]=="E":
      r+=1
print(min(DP))

Solution (Python):
N=int(input())
S=input()
DP=[0]*N
l=0
for i,x in enumerate(S):
  DP[i]+=l
  if x=="E":
    l+=1
r=0
for i,y in enumerate(S[::-1]):
  DP[N-1-i]+=r
  if y=="W":
    r+=1
print(N-1-max(DP))

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

ruir=[0]*N
ruil=[0]*N


for i in range(1,N):
    nowr=ruir[i-1]
    if S[i-1]=="W":
        nowr+=1
    ruir[i]=nowr

    nowl=ruil[-i]
    if S[-(i)]=="E":
        nowl+=1
    ruil[-(i+1)]=nowl

ans=10**6
for i in range(N):
    ans=min(ans, ruir[i]+ruil[i])

print(ans)

Solution (Python):
n = int(input())
s = list(input())
left = [0] * n
right = [0] * n

for i in range(1, n):
    left[i] = left[i - 1]
    if s[i - 1] == 'W':
        left[i] += 1

for i in range(1, n):
    right[n - i - 1] = right[n - i]
    if s[n - i] == 'E':
        right[n - i - 1] += 1

total = [x + y for x, y in zip(left, right)]
ans = min(total)
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 

def ii(): return int(sys.stdin.readline().rstrip()) 
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) 
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]

def ss(): return sys.stdin.readline().rstrip() 
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) 
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]





n=ii()
arr=ss()
wcnt,ecnt=0,0
ans=10**100
wsum=arr.count("W")
esum=n-wsum

for i in range(n):
    if arr[i]=="W":
        ans=min(wcnt+esum-ecnt,ans)
        

        wcnt+=1
    else:
        ecnt+=1
        ans=min(wcnt+esum-ecnt,ans)
        
print(ans)

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from heapq import heapify, heappop, heappush
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
S = list(input())
c = Counter(S)
w = c["W"]
e = c["E"]
al = w + e


ans = e
num_w, num_e = 0, e
for i in range(N):
    sign = S[i]
    if sign == "W":
        w += 1
        al = al + 1
        num_w += 1
    elif sign == "E":
        num_e -= 1
    ans = min(ans, num_e + num_w)
print(ans)

Solution (Python):
import sys
input=sys.stdin.readline
N = int(input())
S = input()
int_s = [0] * N
cum_sum_w = [0] * (N + 1)
cum_sum_e = [0] * (N + 1)
answers = [0] * N
for i in range(N):
    if (S[i] == 'W'):
        int_s[i] = 1
    else:
        int_s[i] = 0
    cum_sum_w[i + 1] = cum_sum_w[i] + int_s[i]
for i in range(N):
    if (S[i] == 'E'):
        int_s[i] = 1
    else:
        int_s[i] = 0
    cum_sum_e[i + 1] = cum_sum_e[i] + int_s[i]
for i in range(N):
    answers[i] = cum_sum_w[i] + (cum_sum_e[N] - cum_sum_e[i + 1])
print(min(answers))

Solution (Python):
def main():
    n = int(input())
    a = [c == 'E' for c in input()]

    def accumulate(a):
        s = 0
        yield s
        for x in a:
            s += x
            yield s

    *acc_e, = accumulate(a)

    ans = 1 << 30
    for i in range(n):
        ans = min(ans, (i - acc_e[i]) + (acc_e[n] - acc_e[i + 1]))

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import numpy as np
n=int(input())
s=input()
w=list(map(lambda x: x=="W",s))
e=list(map(lambda x: x=="E",s))
w = np.array([0]+w).cumsum()
e = np.array([0]+e[::-1]).cumsum()
e = e[::-1]
ans=n
for i in range(n+1):
    tmp=w[i-1]+e[i]
    ans=min(ans,tmp)
print(ans)

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

L = [0]*N

l = 0
for i, x in enumerate(S):
    L[i] += l
    if x == 'E':
        l += 1
r = 0
for i, x in enumerate(S[::-1]):
    L[N-1-i] += r
    if x == 'W':
        r += 1

print(N-1 - max(L))

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

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

ans = float("INF")
for i in range(1,n+1):
    a = ((i-1) - cnt[i-1]) + (cnt[-1] - cnt[i])
    if ans > a: ans = a
print(ans)

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

l = 0
L = []
r = 0
R = [0] * N
for i in range(N):
  if i == 0:
    L.append(0)
  else:
    if S[i-1] == 'W':
      l += 1
    L.append(l)
    
for i in range(N-1, -1, -1):
  if i == N-1:
    R[i] = 0
  else:
    if S[i+1] == 'E':
      r += 1
    R[i] = r
    
count = []
for i in range(N):
  count.append(L[i] + R[i])

print(min(count))

Solution (Python):
n=int(input())
s=input()
w=[0]*n
e=[0]*n
for i in range(n):
  if s[i]=="W":
    w[i]+=1
  else:
    e[i]+=1
from itertools import accumulate
w=list(accumulate(w))
e=list(accumulate(e))


ans=10**18
for i in range(n):
  t=0
  if s[i]=="W":
    if i!=0:
      t+=w[i-1]
    t+=e[-1]-e[i]
  else:
    if i!=0:
      t+=w[i-1]
    t+=e[-1]-e[i]
  ans=min(t,ans)
print(ans)

Solution (Python):
from collections import Counter
n = int(input())
s = input()

cnt = Counter(s)
W = cnt['W']
E = cnt['E']


l = [[0, 0] for i in range(n + 1)]

dict = {'E':0, 'W':1}
for i in range(n):
    temp1, temp2 = l[i]
    l[i + 1] = [temp1, temp2]
    l[i + 1][dict[s[i]]] += 1

count = 10 ** 7
for i in range(n):
    e, w = l[i + 1]
    temp = E - e + w if s[i] != 'W' else E - e + w - 1
    if count > temp:count = temp
    
print(count)

Solution (Python):
N=int(input())
S=input()
l=0
r=S[1:].count("E")

answer=N

for i in range(N-1):
    if l+r<answer:
        answer=l+r
    if S[i]=="W":
        l+=1
    if S[i+1]=="E":
        r-=1
if l+r<answer:
    answer=l+r
print(answer)

Solution (Python):
n=int(input())
l=input()
ans=[0]*n
wc=0
ec=0
for i in range(n-1):
  if l[i]=="W":
    wc+=1
  ans[i+1]+=wc
  if l[n-1-i]=="E":
    ec+=1
  ans[n-2-i]+=ec

print(min(ans))

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

Solution (Python):
n = int(input())
S = [int(c == 'W') for c in list(input())]
L = [0]
for i in range(n):
    L.append(L[-1]+S[i])
R = [0]
for i in range(n-1, 0, -1):
    R.append(R[-1]+1-S[i])
R.reverse()
print(min([L[i]+R[i] for i in range(n)]))

Solution (Python):
N = int(input())
S = input().strip()
E = [0 for _ in range(N+1)]
W = [0 for _ in range(N+1)]
for i in range(N):
    if S[i] == "E":
        E[i+1] = E[i]+1
        W[i+1] = W[i]
    else:
        W[i+1] = W[i]+1
        E[i+1] = E[i]
cmin = 10**6
for i in range(N):
    cmin = min(cmin,W[i]+E[-1]-E[i+1])
print(cmin)

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

Solution (Python):
def main():
    n = int(input())
    s = input()
    west = [0 for _ in range(n)]
    west[0] = 1 if s[0] == "W" else 0
    for i in range(1, n):
        if s[i] == "W":
            west[i] = west[i - 1] + 1
        else:
            west[i] = west[i - 1]
    answer = n - west[-1]
    if s[0] == "E":
        answer -= 1
    for i in range(1, n):
        answer = min(answer, west[i - 1] + n - i - west[-1] + west[i - 1])
    tmp = 0 if s[-1] == "E" else 1
    answer = min(answer, west[-1] - tmp)
    print(answer)


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
s = list(input())
total_e = s.count("E")
ansbox = [0]*n
e,w = 0,0
for i in range(n):
    if s[i] == "E":
        e += 1
    ansbox[i] = (total_e - e) + w
    if s[i] == "W":
        w += 1
print(min(ansbox))

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

east = S.count("E")
west = 0

ans = 10 ** 18
for s in S:
    if s == "W":
        ans = min(ans, east + west)
        west += 1
    else:
        east -= 1
        ans = min(ans, east + west)
print(ans)

Solution (Python):
def main():
    N = int(input())
    S = input()
    from collections import Counter
    c = Counter(S)
    ans = 10**9
    past_w = 0
    past_e = 0
    for s in S:
        if s == "W":
            ans = min(ans, past_w + c["E"]-past_e)
            past_w += 1
        else:
            past_e += 1
            ans = min(ans, past_w + c["E"]-past_e)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H):
    '''
    H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) 
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    


MOD = 10**9 + 7
INF = 2**31  

from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  





N = read_a_int()
S = read()[:-1]
n_W = 0
n_E = S.count('E')
ans = 10**6
for s in S:
    if s == 'E':
        n_E -= 1
    ans = min(ans, n_E + n_W)
    if s == 'W':
        n_W += 1
print(ans)

Solution (Python):











import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log 
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N=I()
    S=ST()
    
    left_d = {"E":0, "W":0}
    right_d = Counter(S[:])
    ans = inf
    for i in range(N):
        right_d[S[i]]-=1
        cost = left_d["W"] + right_d["E"]
        ans = min(ans,cost)
        left_d[S[i]]+=1
    print(ans)
        
if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
s_li = list(input())
all_li = [[0,0]]
for s in s_li:
    if s == 'W':
        all_li.append([all_li[-1][0]+1,all_li[-1][1]])
    else:
        all_li.append([all_li[-1][0],all_li[-1][1]+1])

ans = float('inf')
for i,a in enumerate(all_li):
    if i == 0:
        continue
    tmp_count = 0
    if i-1>0:
        tmp_count += all_li[i-1][0]
    tmp_count += (all_li[-1][1]-a[1])
    if tmp_count < ans:
        ans = tmp_count
    
print(ans)

Solution (Python):
import sys
from io import StringIO
import unittest

def resolve():
    n = int(input())
    s = list(input())
    cnt_w = s.count('W')
    cnt_e = s.count('E')

    tmp_w = 0
    tmp_e = 0

    ans = []

    for i in range(n):
        if s[i] == 'W':
            tmp_w += 1
        else:
            tmp_e += 1

        if s[i] == 'W':
            ans.append(tmp_w + cnt_e - tmp_e - 1)
        else:
            ans.append(tmp_w + cnt_e - tmp_e)

    print(min(ans))


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """5
WEEWW"""
        output = """1"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """12
WEWEWEEEWWWE"""
        output = """4"""
        self.assertIO(input, output)
    def test_入力例_3(self):
        input = """8
WWWWWEEE"""
        output = """3"""
        self.assertIO(input, output)

if __name__ == "__main__":
    
    resolve()

Solution (Python):
def main():
    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)
if __name__=="__main__":
    main()

Solution (Python):
n=int(input())
s=input()
re=s[1:].count('E')
ans=re
a=ans
lw=0
for i in range(1,n):
  if s[i]=='E':
    a-=1
  if s[i-1]=='W':
    a+=1
  ans=min(a,ans)

print(ans)

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

left = [0 for _ in range(N)]
cnt = 0
for i in range(0, N):
    left[i] = cnt
    if S[i] == 'W':
        cnt += 1

right = [0 for _ in range(N)]
cnt = 0
for i in range(N - 1, -1, -1):
    right[i] = cnt
    if S[i] == 'E':
        cnt += 1

total = [l + r for l, r in zip(left, right)]
print(min(total))

Solution (Python):
n = int(input())
s = input()
ecnt = [0]*n
wcnt = [0]*n
res = 0
for i in range(1,n):
    
    if s[i-1] == 'E':res +=1
    ecnt[i] = res
res = 0
for i in range(n-2,-1,-1):
    if s[i+1] == 'W':res+=1
    wcnt[i] = res
ans = 0
for i in range(n):
    ans = max(ans,ecnt[i]+wcnt[i])
ans = n-1-ans
print(ans)

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

W,E = [0]*N, [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]

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

A = [0]*N
for i in range(N):
    A[i] = E[i]+W[i]

ans = min(A)
print(ans)

Solution (Python):
def main():
    n = int(input())
    s = input()
    east = [0 for _ in range(n)]
    west = [0 for _ in range(n)]
    if s[0] == "E":
        east[0] = 1
    else:
        west[0] = 1
    for i in range(1, n):
        east[i] = east[i - 1]
        west[i] = west[i - 1]
        if s[i] == "E":
            east[i] += 1
        else:
            west[i] += 1
    answer = east[-1] - east[0]
    for i in range(1, n):
        answer = min(answer, west[i - 1] + east[-1] - east[i])
    print(answer)


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
s = input()
wr = s.count('W')
er = n-wr
wl = 0
el = 0
ans = 10**10
for i in range(n):
    if s[i] == 'W':
        wr -= 1
        ans = min(ans,er+wl)
        wl += 1
    else:
        er -= 1
        ans = min(ans,er+wl)
        el += 1
print(ans)

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

cnt_left = 0
cnt_right = s[1:].count('E')
ret = cnt_left + cnt_right

for i in range(1, n):
    if s[i] == 'E':
        cnt_right -= 1
    if s[i-1] == 'W':
        cnt_left += 1
    ret = min(ret, cnt_left + cnt_right)

print(ret)

Solution (Python):
def resolve():
    '''
    code here
    '''
    N = int(input())
    S = input()

    W_list = [0 for _ in range(N)]
    E_list = [0 for _ in range(N)]
    
    for i, s in enumerate(S):
        if i == 0:
            if s == 'W':
                W_list[i] = 1
        else:
            W_list[i] = W_list[i-1]
            if s == 'W':
                W_list[i] += 1

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

    sum_list = []
    for i in range(N):
        sum_list.append(E_list[i] + W_list[i])

    res = min(sum_list) -1
    print(res)


if __name__ == "__main__":
    resolve()

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

s = S[::-1]
w = [0]*(N+1)
e = [0]*(N+1)
cnt_w = 0
cnt_e = 0

for i in range(N):
    if(S[i] == "W"):
        cnt_w += 1
        w[i+1] = cnt_w
    else:
        w[i+1] = cnt_w

for i in range(N):
    if(s[i] == "E"):
        cnt_e += 1
        e[i+1] = cnt_e
    else:
        e[i+1] = cnt_e

e = e[::-1]        
ans = 2*N

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

Solution (Python):
n = int(input())
s = input()
inv_s = s[::-1]
lst_e =[]
lst_w =[]
e = w = 0
for i in range(n):
  if s[i] == 'W':
    w += 1
  if inv_s[i] == 'E':
    e += 1
  lst_e.append(e)
  lst_w.append(w)
lst_w.reverse()


ans = [lst_e[i]+lst_w[i] for i in range(n)]
print(min(ans)-1)

Solution (Python):
n = int(input())
s = input()
ww = []
tmp = 0
for i in range(n):
    if s[i] == 'W':
        tmp += 1
    ww.append(tmp)
ee = []
tmp = 0
for i in range(n)[::-1]:
    if s[i] == 'E':
        tmp += 1
    ee.append(tmp)
ee = ee[::-1]
a = [ee[0]] +  [ee[i+1] + ww[i-1] for i in range(1,n-1)] + [ww[-1]]
print(min(a))

Solution (Python):



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


def main():
    left = {"E": 0, "W": 0}
    right = {"E": S.count("E"), "W": S.count("W")}

    ans = N
    for s in S:
        right[s] -= 1
        ans = min(ans, left["W"] + right["E"])
        left[s] += 1
    
    print(ans)


if __name__ == "__main__":
    main()

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

r = [0] * n 
l = [0] * n 

r[0] = 0
l[-1] = 0
for i in range(1,n):
    r[i] = r[i-1] + (s[i-1]=='W')
    l[-i-1] = l[-i] + (s[-i]=='E')

ans = n

for i in range(n):
    ans = min(ans, r[i]+l[i])

print(ans)

Solution (Python):
from itertools import accumulate
n = int(input())
s = input()

l = [0] * n
for i, v in enumerate(s):
    if v == "W":
        l[i] = 1
m = [0] * n
for j, w in enumerate(s):
    if w == "E":
        m[j] = 1

l = list(accumulate(l))
m = list(accumulate(m))

ans = m[-1]
for k in range(1, n):
    ans = min(l[k-1] + m[-1] - m[k], ans)

print(ans)

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

num_W = [0]*N
num_E = [0]*N
for i in range(N):
    if S[i] == "W":
        num_W[i] = 1
    else:
        num_E[i] = 1

for i in range(N-1):
    num_W[i+1] += num_W[i]
    num_E[i+1] += num_E[i]

ans = num_E[N-1]

for i in range(N):
    num_E[i] = num_E[N-1] - num_E[i]

for i in range(1, N):
    num = num_W[i-1] + num_E[i] - num_E[N-1]
    ans = min(ans, num)


print ans

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

num_west = [0] * N
for i, s in enumerate(S, 1):
  if i == N:
    continue
  num_west[i] += num_west[i-1]
  if s == 'W':
    num_west[i] += 1

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

ans = N
for nw, ne in zip(num_west, num_east):
  ans = min(ans, nw + ne)

print(ans)

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

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




ans = 3*10**5
for i in range(1, n+1):
    ans = min(ans, W[i-1]+E[-1]-E[i])
    
print(ans)

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

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

for idx, s in enumerate(S):
    if idx == 0:
        continue
    if S[idx-1] == "W":
        now += 1
    if s == "E":
        now -= 1
    ans = min(ans, now)
print(ans)